01 Struts2 入门实例

strtus2搭建步骤:
    1.拷贝所需jar到WEB工程目录下的libwKiom1ZSfV7Se9KIAACZFl9siTo902.png

    2.配置WEB.xml文件,配置过滤器Filter

        <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>

    3.编写login.jsp:        

        <form action="login.action" method="post">
        <h1>用户登录</h1>
        账号:<input type="text" name="userName"/><br/>
        密码:<input type="password" name="passWord"><br/>
        <input type="submit" value="登录"/>
        </form>

     4.创建LoginAction类继承ActionSupport类,重写execute()

package com.liu.web.action;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 用户登录的action
 * @author Administrator
 *
 */
public class LoginAction extends ActionSupport{
    private String userName;
    
    private String passWord;
    
    @Override
    public String execute() throws Exception {
        
        System.out.println("账号:"+userName);
        System.out.println("密码:"+passWord);
        
        return "success";
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassWord() {
        return passWord;
    }
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
}

        5.拷贝struts.xml配置文件到src目录下:

   
        <constant name="struts.enable.DynamicMethodInvocation" value="false" />
        <constant name="struts.devMode" value="true" />
        <package name="default" namespace="/" extends="struts-default">
            <action name="login" class="com.liu.web.action.LoginAction">
                <result name="success">/success.jsp</result>
            </action>
        </package>


你可能感兴趣的:(struts2,过滤器,工程,用户登录)