乐者为王

Do one thing, and do it well.

Struts 2登录实例

新的Eclipse已经包含Maven插件了,不过还需要到Eclipse Marketplace中去安装一个m2-wtp(Maven Integeration for Eclipse WTP)插件,不然启动Tomcat时会报错。

安装好后创建新Maven项目,填写groupId和artifactId。

在pom.xml中加入Struts 2的依赖配置:

1
2
3
4
5
6
7
<dependencies>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.3.4</version>
    </dependency>
</dependencies>

在web.xml文件中添加Struts 2的Filter:

1
2
3
4
5
6
7
8
9
10
11
<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>

配置resources/struts.xml文件:

1
2
3
4
5
6
7
8
9
10
11
12
<struts>
    <package name="default" extends="struts-default">
        <action name="login" class="com.codemany.account.action.LoginAction">
            <result name="success">/index.jsp</result>
            <result name="input">/index.jsp</result>
        </action>

        <action name="logout" class="com.codemany.account.action.LogoutAction">
            <result name="success" type="redirect">/index.jsp</result>
        </action>
    </package>
</struts>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.codemany.account.action;

import com.codemany.account.model.User;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    private static final long serialVersionUID = -389833745243649130L;

    private String username;
    private String password;

    public String execute() throws Exception {
        if (username == null || username.length() != 0
                || password == null || password.length() != 0) {
            return INPUT;
        }

        if ("test".equals(username) && "test".equals(password)) {
            ActionContext.getContext().getSession().put("logined", true);
            return SUCCESS;
        }

        return INPUT;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.codemany.account.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LogoutAction extends ActionSupport {
    private static final long serialVersionUID = -518996422060716751L;

    public String execute() throws Exception {
        ActionContext.getContext().getSession().put("logined", false);
        return SUCCESS;
    }
}

index.jsp内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>

<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Struts2 Login Sample</title>
</head>

<body>
    <s:if test="#session.logined">
        Welcome, you have logined. <a href="<%= request.getContextPath() %>/logout.action">Logout</a>
    </s:if>
    <s:else>
        <p>User: test/test</p>
        <s:form action="login">
            <s:textfield key="username" label="Username" />
            <s:password key="password" label="Password" />
            <s:submit value="Login" />
        </s:form>
    </s:else>
</body>
</html>

在项目上点击右键 -> Run As -> Maven build运行项目。在Goals栏中填写tomcat:run,Maven会自动下载Tomcat到项目的target目录下后启动服务器。

代码下载:https://github.com/dohkoos/JBookShelf

Comments