struts2的入门程序

来源:互联网 发布:网路优化 编辑:程序博客网 时间:2024/06/05 03:25

1:写一个登陆表单

<s:form action="login">    <s:textfield name="username" key="user"/>    <s:textfield name="password" key="pass"/>    <s:submit key="login"/></s:form>

2:配置好过滤器和strust.xml

    <package name="struts2" namespace="/" extends="struts-default">        <action name="login" class="com.LoginAction">            <!-- 定义三个逻辑视图和物理资源之间的映射 -->            <result name="success">/WEB-INF/welcome.jsp</result>                <result name="error">/WEB-INF/content/error.jsp</result>        </action>    </package>

4:写好action:

public class LoginAction extends ActionSupport{    // 定义封装请求参数的username和password成员变量    private String username;    private String password;        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;    }    // 定义处理用户请求的execute方法    public String execute() throws Exception    {        // 当username为crazyit.org,password为leegang时即登录成功        if (this.getUsername().equals("123")            && this.getPassword().equals("456") )        {            ActionContext.getContext().getSession()                .put("user" , this.getUsername());            return SUCCESS;        }        return ERROR;    }}

访问表单登陆页面,输入用户名123 密码456 ,成功跳转至登陆成功页面,输入其他跳转至错误页面。入门程序书写成功。

原创粉丝点击