实战struts2——用户登录
内容提要:本文通过“用户登录”这个简单功能,介绍struts2的标志(Tag)、Action、输入校验(Input Validation)以及本地化输出(Localizing Output)。
开发环境: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 >
这里,我们用到的是这句:< 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>
<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;
}
}
需要注意这句: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配置)return INPUT;
if (isInvalid(getPassword()))
return INPUT;
return SUCCESS;
现在我们可以回想上面出现的“!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>
"-//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>
其中:"-//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”,这是通配符式的写法。class="com.cleversoft.struts2.demo.Logon">
<result type="redirect-action">Menu</result>
<result name="input">/Logon.jsp</result>
</action>
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>
<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)} is required.
password = Password
username = User Name
password = Password
username = User Name
接着需要修改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>
"-//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>
<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>
参考资料:
1. Getting Started
2. http://www.blogjava.net/max/category/16130.html