開發環境:myeclipse5.0+eclipse3.2+jdk5.0+tomcat5.5+struts2+junit3.8(同實戰 struts2——Hello World)
項目文件:
1.首先我們為“用戶登錄”提供一個簡單入口
Welcome.jsp:
<% @ taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
< title > Welcome </ title >
< link href ="<s:url value=" /css/tutorial.css" /> " rel="stylesheet" type="text/css" />
</ head >
< body >
< h3 >
Commands
</ h3 >
< ul >
< li >
< a href ="<s:url action=" Register" /> ">Register </ a >
</ li >
< li >
< a href ="<s:url action=" Logon!input" /> ">Sign On </ a >
</ li >
</ ul >
</ body >
</ html >
這裡,我們用到的是這句:
<a href="<s:url action="Logon!input"/>">Sign On</a>
暫時先不理會“!input”的具體意義,接著往下看。
2.登錄頁面
Logon.jsp:
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Login</title>
</head>
<body>
<s:form action="Logon">
<s:textfield label="User Name" name="username" />
<s:password label="Password" name="password" />
<s:submit />
</s:form>
</body>
</html>
3.接著,編寫與之對應的Action類。
com.cleversoft.struts2.demo.Logon.java:
package com.cleversoft.struts2.demo;
import com.opensymphony.xwork2.ActionSupport;
public class Logon extends ActionSupport {
/** *//**
*
*/
private static final long serialVersionUID = -9039794122089833258L;
public String execute() throws Exception {
if (isInvalid(getUsername()))
return INPUT;
if (isInvalid(getPassword()))
return INPUT;
return SUCCESS;
}
private boolean isInvalid(String value) {
return (value == null || value.length() == 0);
}
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
需要注意這句:
if (isInvalid(getUsername()))
return INPUT;
if (isInvalid(getPassword()))
return INPUT;
return SUCCESS;
如果用戶名和密碼均不為空的話,返回“success”,響應Menu.jsp頁面;否則返回“ input”,響應Logon.jsp頁面。(見隨後struts.xml中的Action配置)
現在我們可以回想上面出現的“!input”。
4.以上提到返回“input”,那接著看看與之對應的輸入校驗頁面。
Logon-validation.xml:
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="username">
<field-validator type="requiredstring">
<message>Username is required</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>Password is required</message>
</field-validator>
</field>
</validators>
需要注意的是,命名需要與與其對應的Action類匹配。
5.最後需要進行Action配置。
struts.xml:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="com.cleversoft.struts2.demo"
extends="struts-default">
<action name="HelloWorld"
class="com.cleversoft.struts2.demo.HelloWorld">
<result>/HelloWorld.jsp</result>
</action>
<action name="Welcome">
<result>/Welcome.jsp</result>
</action>
<action name="Menu">
<result>/Menu.jsp</result>
</action>
<action name="Logon!*" method="{1}"
class="com.cleversoft.struts2.demo.Logon">
<result type="redirect-action">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
</package>
</struts>
其中:
<action name="Logon!*" method="{1}"
class="com.cleversoft.struts2.demo.Logon">
<result type="redirect-action">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
“method="{1}"”等價於“method=input”,這是通配符式的寫法。
6.其他
Menu.jsp:
<html>
<head>
<title>Missing Feature</title>
</head>
<body>
<p>
This feature is under construction.Please try again in the next interation.
</p>
</body>
</html>
7.運行
訪問http://localhost:8080/struts2/Welcome.action
運行結果:
登錄
輸入校驗
登錄成功
8.本地化輸出
首先需要添加資源文件
package.properties:
requiredstring=$\{getText(fieldName)}isrequired.
password=Passw ord
username=UserN ame
接著需要修改Logon-validation.xml和Logon.jsp
Logon-validation.xml:
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="username">
<field-validator type="requiredstring">
<message key="requiredstring"/>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message key="requiredstring"/>
</field-validator>
</field>
</validators>
Logon.jsp:
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Login</title>
</head>
<body>
<s:form action="Logon">
<s:textfield label="%{getText('username')}" name="username"/>
<s:password label="%{getText('password')}" name="password" />
<s:submit />
</s:form>
</body>
</html>