1、Struts2的一个登陆实例

1、新建项目,导入包
2、com.action.StrutsAction

public class StrutsAction extends ActionSupport {
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private String name;
    @Override
    public String execute() throws Exception {
       if (!name.equals("hello")){
           Map request= (Map) ActionContext.getContext().get("request");
           request.put("name", getName());
           return  SUCCESS;
       }else {
           return  ERROR;
       }
    }
}

3、配置struts.xml

<struts>
    <package name="com.action" extends="struts-default ">
        <action name="StrutsAction" class="com.action.StrutsAction" method="execute">
            <result name="success">/welcome.jsp</result>
            <result name="error">/index.jsp</result>
        </action>
    </package>
</struts>

4、配置web.xml

 <welcome-file-list>
        <welcome-file>hello.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>

5、新建hello.jsp登陆表单,点击登录指向StrutsAction

<body>
<form action="StrutsAction" method="post">
    请输入姓名:<input type="text" name="name">
    <input type="submit" value="提交">
</form>
</body>

6、新建welcome.jsp
StrutsAction携带值指向welcome.jsp

<body>
欢迎<%=request.getAttribute("name")%>
</body>

猜你喜欢

转载自blog.csdn.net/nba_linshuhao/article/details/82628788