struts2 实例

下面我们通过实例来学习一下Struts2的使用。通过本实例的学习,我们将会对struts2的表单以及表单验证有一个初步的认识、了解struts2的配置以及初探Struts2的本地化输出。
1.实例说明
本例是Struts2的简单实例,通过本工程的学习,我们将会对struts2的表单以及表单验证有一个初步的认识、了解struts2的配置以及初探Struts2的本地化输出。

2. 编码准备
1)包的引入
在MyEclipse或NetBeans中建立web工程,将所需的包放入WebRoot/lib目录中,本实例所需的包有:
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.8.jar
xwork-2.0.3.jar
这些包在struts-2.0.8-all/struts-2.0.8/lib目录下都可以找到,请读者自行下载。
2) web.xml的配置
要使struts能正常工作,需修改web.xml的内容,为其增加struts2的FilterDispatcher,修改后的web.xml的内容如下:


3)建立源码目录和jsp存放目录
在src目录下建立example文件夹,用于存放本实例的java文件等。在WebRoot文件夹下建立子文件夹example,用于存放本工程的jsp文件。

3. 编码
1) Welcome.jsp和Login.jsp的编写
首先我们建立Welcome.jsp,该文件包含两个链接,点击“登录”链接后跳转到登录信息输入页面,点击“注册”按钮跳转到注册页,为了学习struts2配置中的通配符使用,我们暂不实现注册功能。Welcome.jsp的代码如下:

       
       
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>欢迎界面</title> <link href="<s:urlvalue="/css/examplecss"/> "rel="stylesheet" type="text/css"/> </head> <body> <h3>导航</h3> <ul> <li><a href="<s:urlvalue="/example/Login.jsp"/>">登录</a></li> <li><a href="<s:urlaction="Register"/>">注册</a></li> </ul> </body> </html>


    在该页面的顶部,我们需要将struts2的标签库引入,语句为:<%@ taglib prefix="s" uri="/struts-tags" %>
在该页面,主要用到struts2的<s:url>标签,该页面主要用到该标签的两个属性,分别为value和action,其中action属性表示用action来产生url,而value表示使用的目标值。在页面上点击“查看源文件”按钮,可看到生成的语句分别变为:
<link href="/struts2-blank-2.0.8/css/examplecss" rel="stylesheet" type="text/css"/>
<a href="/struts2-blank-2.0.8/example/Login.jsp ">
<a href="/struts2-blank-2.0.8/example/Register.action">
由此可知使用该标签时,struts2会自动为我们带上下文路径,对于加了属性action的<s:url>标签,后面会自动带上“.action”作为后缀。
点击“登录”链接后,跳转到Login.jsp页面,该页包含一个登录表单,让用户输入用户名和密码信息,用户点击提交按钮,跳转到指定的Action——Login进行处理。Login.jsp的内容如下:

       
       
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>登录</title> </head> <body> <s:form action="Login"> <s:textfield name="username" label="用户名"/> <s:password name="password" label="密码"/> <s:submit/> </s:form> </body> </html>


该页用到Struts2的表单标签<s:form>、<s:textfield>和<s:password>。
<s:form>的action属性表示表单提交后跳转的action的名称,此处为Login,该标签最终将生成HTML的form;
<s:textfield>标签类同于HTML的<input type=”text” …>,其中name表示属性域的名称,label表示其前的提示名;
<s:password>标签类同于HTML的<input type=”password” …>,其name和label类同于<s:textfield>,在此略。

2)配置文件struts.xml和example.xml
在上述jsp页面,我们需跳转到两个Action地址,需在struts2的配置文件中配置,因当工程变大时,一个庞大的struts2的配置极难维护,建议按包路径分开配置文件,所以本实例除了struts.xml配置文件外,还新增了一个额外的配置文件example.xml。该文件在struts.xml中引用。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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <include file="example.xml"/> <!-- Add packages here --> </struts>


可看到该文件通过<include file="example.xml"/>将example.xml也作为struts2的配置文件。
接下来,让我们看看example.xml的配置:

       
       
<?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> <package name="example" namespace="/example" extends="struts-default"> <action name="Login_input" method="{1}" class="example.Login"> <result name="input">/example/Login.jsp</result> <result type="redirect-action">Menu</result> </action> <!-- 为学习struts2配置文件中通配符的使用,我们将未定义的action的引用都定向到example.ExampleSupport这个Action中, --> <!-- 需定向的Action的名字传到{1}中,eg.若请求Register这个action,当ExampleSupport返回success时,跳转到/example/Register.jsp --> <action name="*" class="example.ExampleSupport"> <result>/example/{1}.jsp</result> </action> </package> </struts>


3) Login和ExampleSupport类以及验证配置类Login-validation.xml的编写
在配置文件example.xml中,定义了两个Action,下面我们用代码来实现这两个Action
首先让我们来看看ExampleSupport这个Action,这个Action不做任何操作,集成自ActionSupport,是本工程的各Action类的基类,该类的代码如下:
package example;
import com.opensymphony.xwork2.ActionSupport;
publicclass ExampleSupport extends ActionSupport {
}

接着让我们来看看Login这个Action,该类继承自ExampleSupport类,该Action需实现的业务逻辑如下:
a)         当用户名(username)或密码(password)有一者或两者为空时,登录不成功,跳转到登录信息输入页面;
b)        当用户名(username)和密码(password)都不为空时,登录成功,跳转到主菜单页。
对于用户名和密码的验证,我们可以先考虑在Login类中用代码实现的方式,此时该类的代码如下:

       
       
package example; public class Login extends ExampleSupport { 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; } }


    当工程变得愈发复杂时,这一小段一小段验证代码将会变得难以维护,出于此原因,我们可以考虑采用struts2提供的验证机制来实现。在src目录下建立实现验证的xml文件Login-validation.xml,为Login Action中的username和password属性增加非空验证,验证配置如下:


当验证未通过时,将不会进入Login Action中的execute方法,此时可删除掉Login这个Action中的验证内容,该类的execute方法直接跳转到SUCCESS即可,修改后的代码略。

4)本地化输出——资源文件package.properties
为了本地化的输出验证错误信息,我们可以将参数信息和错误信息放入资源文件中,资源文件package.properties位于src/example目录下,内容如下:
requiredstring = ${getText(fieldName)}不能为空.
password = 密码
username = 用户名
Missing.message = 该部分尚未构建,请稍候访问...
在src/example目下下建立对应的中文资源文件package_zh_CN.properties,为了避免中文乱码问题,我们编写一个批处理文件code.bat来对package.properties进行编码处理,主要用到native2ascii命令,其内容如下:
del package_zh_CN.properties
copy package.properties package_zh_CN.properties.gbk
native2ascii -encoding GBK package_zh_CN.properties.gbk package_zh_CN.properties
del package_zh_CN.properties.gbk
del *.bak
运行该批处理文件,可在package_zh_CN.properties中可看到编码后的资源文件信息,如下:
requiredstring = ${getText(fieldName)}/u4e0d/u80fd/u4e3a/u7a7a.
password = /u5bc6/u7801
username = /u7528/u6237/u540d
Missing.message = /u8be5/u90e8/u5206/u5c1a/u672a/u6784/u5efa/uff0c/u8bf7/u7a0d/u5019/u8bbf/u95ee...

5)Register.jsp和Missing.jsp的编写
在2中的example.xml中,我们配置了通配符映射,在Welcome.jsp中,我们使用
<a href="<s:url action="Register"/>">注册</a>
其中的Register在example中找不到相关映射,于是在用户点击“注册”按钮时,将映射到通配符所映射的Action:example. ExampleSupport.而后跳转到Register.jsp页面,其代码如下:

       
       
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <s:include value="Missing.jsp"/> 该页面包含Missing.jsp页面,其代码如下: <%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head><title>未构建页面</title></head> <body> <p> <!-- 读取配置文件中的对应信息. --> <s:text name="Missing.message"/> </p> </body> </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值