首页 > 技术文章 > java里Struts2学习登录练习详解

w5942066 2017-03-05 21:18 原文

  最近在学struts2里面遇到很多错误,今天跟大家分享一下,我的开发工具是Eclipse;

1、到网上下载Struts2的包,这里不再累赘,百度有很多;

2、新建一个项目,记得后面加上web.xml文件;

3、先部署struts2开发环境。

    (1)、在struts2.2以后,我们需要导入的包有以下几个:
    commons-fileupload-1.2.1.jar
    commons-io-1.3.2.jar
    commons-logging-1.0.4.jar
    commons-lang3-3.1.jar
    freemarker-2.3.16.jar
    javassist-3.7.ga.jar
    ornl-3.0.jar
    struts2-core-2.2.1.1.jar,
    xwork-core-2.2.1.1jar

    我通常都是把这些文件复制到WEB-INF/lib目录里。也可以在项目属性的Java Build Path中的libraries里面添加;

 

    (2)、在lib文件夹下面的web.xml文件添加以下内容:

    

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 

 

      现在web.xml文件是这样的

    

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Struts2Mianshi</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 
</web-app>

 

 

    (3)、在src目录下新建struts.xml文件,记住配置文件位置 在src目录下,命名struts.xml  小写。配置struts.xml文件

  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    <package name="main" extends="struts-default">
       <!--这个里面写action配置-->
    </package>
</struts>

 

4、在WebContent建一个文件夹存放JSP页面的(我建议还是建立一个Page文件夹  有利于文件的管理,还有后面的路径也好弄)

5、在Page文件里创建一个JSP文件命名index.jsp写登陆界面

<%@ page language="java"  contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    String RootPath=request.getContextPath();
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆界面</title>
</head>
<body>
    <form action="<%=RootPath %>/login" method="post">
        登陆<br/>
        账号:<input type="text" name="username" /><br />
        密码:<input type="password" name="userpwd" /><br /> 
        <input type="submit" value="提交"/>
    </form>
</body>
</html>

<%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径。

 

6、在src下创建一个包在创建一个loginAction.java 让这个类继承ActionSupport(action文件命名规则自己看)

 

看到JSp页面的账号username与密码userpws不。这边要获取就要命名跟他一样

private String UserName;并get、set 然后写execute()方法

 

package com.weiyang.acction;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class loginAction extends ActionSupport {
    private String username;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getUserpwd() {
        return userpwd;
    }
    public void setUserpwd(String userpwd) {
        this.userpwd = userpwd;
    }
    private String userpwd;
    
     /**
     * execute方法会在该Action类被调用的时候自动执行,
     * 如果 账号="admin"并且密码="123456",就返回SUCCESS
     * 否则返回ERROR
     */
     public String execute()
     {
         if(username.equalsIgnoreCase("admin") && userpwd.equalsIgnoreCase("123456"))
         {
             return SUCCESS;
         }
         else
             
         {
             return ERROR;
         }
     }
}

 

7、action写好了就去配置struts.xml

 

<!--action标签的name是login,这个必须跟index.jsp中的action属性一致性。class是loginAction类的全称-->
            <action name="login" class="com.weiyang.acction.loginAction">
                <!--这个标签的意思是当LogAction类的execute方法返回SUCCESS时,页面跳转到success.jsp-->
                <result name="success">WEB-INF/success.jsp</result>
                <result name="error">WEB-INF/error.jsp</result>
            </action>

 

 

login,这个必须跟index.jsp中的action属性一致性。class必须写loginAction类的全称
success 跟error是jsp文件 用来验证成功与失败 把他们新建在WEB-INF目录下
<result name="success">WEB-INF/success.jsp</result>  其他目录建立的写好路径

我的文件结构

 

个人理解的流程,web.xml文件中加入了strtus配置文件,所以index.jsp运行后指向action里面的login,在struts.xml文件找到login通过class找到action类,进行execute方法根据返回的success或者error又指向对应的jsp文件显示给用户。

开始做的时候  出现额404错误  

<!--<constant  name="struts.action.extension" value="action,do"></constant>-->  我在struts.xm文件中有这句运行index.jsp出现404

 

 

action="<%=RootPath %>/login" method="post">  没有获取项目路径也是404,就是像这样action="login" method="post">  

因为我建了一个文件夹PageHH存放jsp文件 所以路径就出现问题

 

最多的还是上面说的细节  命名,struts.xml文件里的配置不仔细出现的404

 

 

目前遇到的问题  在struts.xml文件包含了一个login.xml文件 让他代替struts.xml配置实现登陆模块,以后可以根据各自的功能建不同的配置文件。

<include file="login.xml"></include>

这句加上去  login.xml文件配置也没发现村 运行起来就出现404  还在找问题

 

初学者  大神勿喷!

 

推荐阅读