(1)創建一個Web項目工程Login,添加Struts2所需的所有JAR文件,然後配置文件struts.xml。該文件放置到src目錄下,代碼如下:
<?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>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<action name="LoginAction" class="cn.com.struts2.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
(2)創建登錄頁面Login.jsp,代碼如下:
<%@ page language="java" contentType="text/html; charset=GB2312" pageEncoding="GB2312" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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=ISO-8859-1">
<title>My JSP 'index.jsp' starting page</title>
<s:head theme="ajax" />
</head>
<body>
<s:form action="LoginAction" method="post">
<s:label value="登錄"></s:label>
<s:textfield name="account" label="請輸入用戶名"></s:textfield>
<s:password name="password" label="請輸入密碼"></s:password>
<s:submit value="登錄" ></s:submit>
</s:form>
</body>
</html>
(3)登錄後的跳轉頁面為success.jsp,代碼如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="struts" %>
<!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>My JSP 'index.jsp' starting page</title>
<struts:head theme="ajax" />
</head>
<body>
登錄成功,歡迎你,<struts:property value="account" />
</body>
</html>
在以上兩個頁面中使用了Struts2標簽,其使用時只需要在頁面開頭部分添加如下語句:
<%@ taglib uri="/struts-tags" prefix="struts" %>
(4)創建了登錄頁面和登錄成功後的頁面後,還需要設置Action,項目中的Action為LoginAction.java,代碼如下:
package cn.com.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String account;
private String password;
public String execute() {
if("123456".equalsIgnoreCase(account)&&"123456".equals(password))
{
return SUCCESS;
}
return INPUT;
}
public String getAccount()
{
return account;
}
public void setAccount(String account)
{
this.account = account;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password =password;
}
/* @Override
public void validate() {
// TODO Auto-generated method stub
if(account==null||"".equals(account))
{
this.addFieldError("account", "用戶名不能為空!");
}
else if(account.length()!=6)
{
this.addFieldError("account","用戶名必須為6位");
}
if(password==null||"".equals(password))
{
this.addFieldError("password","密碼不能為空!");
}
else if(password.length()<6||password.length()>12)
{
this.addFieldError("password","密碼長度必須在6到12位之間!");
}
}
*/
}
(5)在Action中設置了驗證的登錄用戶名為“action”,密碼為“1234”,如果登錄的用戶名和密碼和設置的用戶名密碼匹配,則跳轉到success.jsp。最後需要在配置文件web.xml中進行配置,web.xml的代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>