内容提要:本文通过“用户登录”这个简单功能,介绍struts2的标志(Tag)、Action、输入校验(Input Validation)以及本地化输出(Localizing Output)。

开发环境:myeclipse5.0+eclipse3.2+jdk5.0+tomcat5.5+struts2+junit3.8(同​​实战struts2——Hello World​​)


项目目录结构

实战struts2——用户登录_输入校验











实战struts2——用户登录_输入校验_02










项目文件

1. 首先我们为“用户登录”提供一个简单入口

Welcome.jsp:

实战struts2——用户登录_输入校验_03实战struts2——用户登录_struts_04<% 实战struts2——用户登录_struts_05@ taglib prefix = " s " uri = " /struts-tags " %>

实战struts2——用户登录_struts_06< html >

实战struts2——用户登录_struts_06 < head >

实战struts2——用户登录_struts_06 < title > Welcome </ title >

实战struts2——用户登录_struts_06 < link href ="<s:url value=" /css/tutorial.css" /> " rel="stylesheet" type="text/css" />

实战struts2——用户登录_struts_06 </ head >

实战struts2——用户登录_struts_06 < body >

实战struts2——用户登录_struts_06 < h3 >

实战struts2——用户登录_struts_06 Commands

实战struts2——用户登录_struts_06 </ h3 >

实战struts2——用户登录_struts_06 < ul >

实战struts2——用户登录_struts_06 < li >

实战struts2——用户登录_struts_06 < a href ="<s:url action=" Register" /> ">Register </ a >

实战struts2——用户登录_struts_06 </ li >

实战struts2——用户登录_struts_06 < li >

实战struts2——用户登录_struts_06 < a href ="<s:url action=" Logon!input" /> ">Sign On </ a >

实战struts2——用户登录_struts_06 </ li >

实战struts2——用户登录_struts_06 </ ul >

实战struts2——用户登录_struts_06 </ body >

实战struts2——用户登录_struts_06</ html > 这里,我们用到的是这句:

实战struts2——用户登录_struts_06<a href="<s:url action="Logon!input"/>">Sign On</a> 暂时先不理会“!input”的具体意义,接着往下看。


2. 登录页面

Logon.jsp:

实战struts2——用户登录_输入校验_03实战struts2——用户登录_struts_04<%实战struts2——用户登录_struts_05@ taglib prefix="s" uri="/struts-tags"%>

实战struts2——用户登录_struts_06<html>

实战struts2——用户登录_struts_06 <head>

实战struts2——用户登录_struts_06 <title>Login</title>

实战struts2——用户登录_struts_06 </head>

实战struts2——用户登录_struts_06 <body>

实战struts2——用户登录_struts_06 <s:form action="Logon">

实战struts2——用户登录_struts_06 <s:textfield label="User Name" name="username" />

实战struts2——用户登录_struts_06 <s:password label="Password" name="password" />

实战struts2——用户登录_struts_06 <s:submit />

实战struts2——用户登录_struts_06 </s:form>

实战struts2——用户登录_struts_06 </body>

实战struts2——用户登录_struts_06</html>

3. 接着,编写与之对应的Action类。

com.cleversoft.struts2.demo.Logon.java:

实战struts2——用户登录_struts_06package com.cleversoft.struts2.demo;

实战struts2——用户登录_struts_06

实战struts2——用户登录_struts_06import com.opensymphony.xwork2.ActionSupport;

实战struts2——用户登录_struts_06

实战struts2——用户登录_输入校验_03实战struts2——用户登录_struts_04public class Logon extends ActionSupport 实战struts2——用户登录_struts_05{

实战struts2——用户登录_struts_48

实战struts2——用户登录_struts_49实战struts2——用户登录_输入校验_50 /** *//**

实战struts2——用户登录_struts_48 *

实战struts2——用户登录_html_52 */

实战struts2——用户登录_struts_48 private static final long serialVersionUID = -9039794122089833258L;

实战struts2——用户登录_struts_48

实战struts2——用户登录_struts_49实战struts2——用户登录_输入校验_50 public String execute() throws Exception 实战struts2——用户登录_struts_05{

实战struts2——用户登录_struts_48

实战struts2——用户登录_struts_48 if (isInvalid(getUsername()))

实战struts2——用户登录_struts_48 return INPUT;

实战struts2——用户登录_struts_48 if (isInvalid(getPassword()))

实战struts2——用户登录_struts_48 return INPUT;

实战struts2——用户登录_struts_48 return SUCCESS;

实战struts2——用户登录_html_52 }

实战struts2——用户登录_struts_48

实战struts2——用户登录_struts_49实战struts2——用户登录_输入校验_50 private boolean isInvalid(String value) 实战struts2——用户登录_struts_05{

实战struts2——用户登录_struts_48 return (value == null || value.length() == 0);

实战struts2——用户登录_html_52 }

实战struts2——用户登录_struts_48

实战struts2——用户登录_struts_48 private String username;

实战struts2——用户登录_struts_48

实战struts2——用户登录_struts_49实战struts2——用户登录_输入校验_50 public String getUsername() 实战struts2——用户登录_struts_05{

实战struts2——用户登录_struts_48 return username;

实战struts2——用户登录_html_52 }

实战struts2——用户登录_struts_48

实战struts2——用户登录_struts_49实战struts2——用户登录_输入校验_50 public void setUsername(String username) 实战struts2——用户登录_struts_05{

实战struts2——用户登录_struts_48 this.username = username;

实战struts2——用户登录_html_52 }

实战struts2——用户登录_struts_48

实战struts2——用户登录_struts_48 private String password;

实战struts2——用户登录_struts_48

实战struts2——用户登录_struts_49实战struts2——用户登录_输入校验_50 public String getPassword() 实战struts2——用户登录_struts_05{

实战struts2——用户登录_struts_48 return password;

实战struts2——用户登录_html_52 }

实战struts2——用户登录_struts_48

实战struts2——用户登录_struts_49实战struts2——用户登录_输入校验_50 public void setPassword(String password) 实战struts2——用户登录_struts_05{

实战struts2——用户登录_struts_48 this.password = password;

实战struts2——用户登录_html_52 }

实战struts2——用户登录_struts_48

实战struts2——用户登录_xml_100} 需要注意这句:

实战struts2——用户登录_struts_06 if (isInvalid(getUsername()))

实战struts2——用户登录_struts_06 return INPUT;

实战struts2——用户登录_struts_06 if (isInvalid(getPassword()))

实战struts2——用户登录_struts_06 return INPUT;

实战struts2——用户登录_struts_06 return SUCCESS; 如果用户名和密码均不为空的话,返回“success”,响应Menu.jsp页面;否则返回“input”,响应Logon.jsp页面。(见随后struts.xml中的Action配置)

现在我们可以回想上面出现的“!input”。


4. 以上提到返回“input”,那接着看看与之对应的输入校验页面。

Logon-validation.xml:

实战struts2——用户登录_struts_06<!DOCTYPE validators PUBLIC

实战struts2——用户登录_struts_06"-//OpenSymphony Group//XWork Validator 1.0.2//EN"

实战struts2——用户登录_struts_06"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

实战struts2——用户登录_struts_06

实战struts2——用户登录_struts_06<validators>

实战struts2——用户登录_struts_06 <field name="username">

实战struts2——用户登录_struts_06 <field-validator type="requiredstring">

实战struts2——用户登录_struts_06 <message>Username is required</message>

实战struts2——用户登录_struts_06 </field-validator>

实战struts2——用户登录_struts_06 </field>

实战struts2——用户登录_struts_06 <field name="password">

实战struts2——用户登录_struts_06 <field-validator type="requiredstring">

实战struts2——用户登录_struts_06 <message>Password is required</message>

实战struts2——用户登录_struts_06 </field-validator>

实战struts2——用户登录_struts_06 </field>

实战struts2——用户登录_struts_06</validators>

需要注意的是,命名需要与与其对应的Action类匹配。

5. 最后需要进行Action配置。

struts.xml:

实战struts2——用户登录_struts_06<!DOCTYPE struts PUBLIC

实战struts2——用户登录_struts_06 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

实战struts2——用户登录_struts_06 "http://struts.apache.org/dtds/struts-2.0.dtd">

实战struts2——用户登录_struts_06<struts>

实战struts2——用户登录_struts_06 <package name="com.cleversoft.struts2.demo"

实战struts2——用户登录_struts_06 extends="struts-default">

实战struts2——用户登录_struts_06 <action name="HelloWorld"

实战struts2——用户登录_struts_06 class="com.cleversoft.struts2.demo.HelloWorld">

实战struts2——用户登录_struts_06 <result>/HelloWorld.jsp</result>

实战struts2——用户登录_struts_06 </action>

实战struts2——用户登录_struts_06 <action name="Welcome">

实战struts2——用户登录_struts_06 <result>/Welcome.jsp</result>

实战struts2——用户登录_struts_06 </action>

实战struts2——用户登录_struts_06 <action name="Menu">

实战struts2——用户登录_struts_06 <result>/Menu.jsp</result>

实战struts2——用户登录_struts_06 </action>

实战struts2——用户登录_struts_06 <action name="Logon!*" method="{1}"

实战struts2——用户登录_struts_06 class="com.cleversoft.struts2.demo.Logon">

实战struts2——用户登录_struts_06 <result type="redirect-action">Menu</result>

实战struts2——用户登录_struts_06 <result name="input">/Logon.jsp</result>

实战struts2——用户登录_struts_06 </action>

实战struts2——用户登录_struts_06 </package>

实战struts2——用户登录_struts_06</struts> 其中:

实战struts2——用户登录_struts_06 <action name="Logon!*" method="{1}"

实战struts2——用户登录_struts_06 class="com.cleversoft.struts2.demo.Logon">

实战struts2——用户登录_struts_06 <result type="redirect-action">Menu</result>

实战struts2——用户登录_struts_06 <result name="input">/Logon.jsp</result>

实战struts2——用户登录_struts_06 </action> “method="{1}"”等价于“method=input”,这是通配符式的写法。


6. 其他

Menu.jsp:

实战struts2——用户登录_struts_06<html>

实战struts2——用户登录_struts_06 <head>

实战struts2——用户登录_struts_06 <title>Missing Feature</title>

实战struts2——用户登录_struts_06 </head>

实战struts2——用户登录_struts_06

实战struts2——用户登录_struts_06 <body>

实战struts2——用户登录_struts_06 <p>

实战struts2——用户登录_struts_06 This feature is under construction. Please try again in the next interation.

实战struts2——用户登录_struts_06 </p>

实战struts2——用户登录_struts_06 </body>

实战struts2——用户登录_struts_06</html>

7. 运行

访问​​http://localhost:8080/struts2/Welcome.action​

运行结果:

实战struts2——用户登录_xml_161







登录

实战struts2——用户登录_html_162






输入校验

实战struts2——用户登录_html_163









登录成功

实战struts2——用户登录_html_164






实战struts2——用户登录_html_165




8. 本地化输出

首先需要添加资源文件

package.properties:

实战struts2——用户登录_struts_06requiredstring = $\{getText(fieldName)} is required.

实战struts2——用户登录_struts_06password = Password

实战struts2——用户登录_struts_06username = User Name

接着需要修改Logon-validation.xml和Logon.jsp

Logon-validation.xml:

实战struts2——用户登录_struts_06<!DOCTYPE validators PUBLIC

实战struts2——用户登录_struts_06"-//OpenSymphony Group//XWork Validator 1.0.2//EN"

实战struts2——用户登录_struts_06"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

实战struts2——用户登录_struts_06

实战struts2——用户登录_struts_06<validators>

实战struts2——用户登录_struts_06 <field name="username">

实战struts2——用户登录_struts_06 <field-validator type="requiredstring">

实战struts2——用户登录_struts_06 <message key="requiredstring"/>

实战struts2——用户登录_struts_06 </field-validator>

实战struts2——用户登录_struts_06 </field>

实战struts2——用户登录_struts_06 <field name="password">

实战struts2——用户登录_struts_06 <field-validator type="requiredstring">

实战struts2——用户登录_struts_06 <message key="requiredstring"/>

实战struts2——用户登录_struts_06 </field-validator>

实战struts2——用户登录_struts_06 </field>

实战struts2——用户登录_struts_06</validators>

Logon.jsp:

实战struts2——用户登录_输入校验_03实战struts2——用户登录_struts_04<%实战struts2——用户登录_struts_05@ taglib prefix="s" uri="/struts-tags"%>

实战struts2——用户登录_struts_06<html>

实战struts2——用户登录_struts_06 <head>

实战struts2——用户登录_struts_06 <title>Login</title>

实战struts2——用户登录_struts_06 </head>

实战struts2——用户登录_struts_06 <body>

实战struts2——用户登录_struts_06 <s:form action="Logon">

实战struts2——用户登录_struts_06 <s:textfield label="%{getText('username')}" name="username"/>

实战struts2——用户登录_struts_06 <s:password label="%{getText('password')}" name="password" />

实战struts2——用户登录_struts_06 <s:submit />

实战struts2——用户登录_struts_06 </s:form>

实战struts2——用户登录_struts_06 </body>

实战struts2——用户登录_struts_06</html>


参考资料:

1. ​​Getting Started​

2. ​​http://www.blogjava.net/max/category/16130.html​