java

位置:IT落伍者 >> java >> 浏览文章

Struts2 框架使用实例分析


发布日期:2022年01月04日
 
Struts2 框架使用实例分析

下面我们通过实例来学习一下Struts的使用通过本实例的学习我们将会对struts的表单以及表单验证有一个初步的认识了解struts的配置以及初探Struts的本地化输出

实例说明

本例是Struts的简单实例通过本工程的学习我们将会对struts的表单以及表单验证有一个初步的认识了解struts的配置以及初探Struts的本地化输出

编码准备

)包的引入

在MyEclipse或NetBeans中建立web工程将所需的包放入WebRoot/lib目录中本实例所需的包有

commonsloggingjar

freemarkerjar

ognljar

strutscorejar

xworkjar

这些包在strutsall\struts\lib目录下都可以找到请读者自行下载

) webxml的配置

要使struts能正常工作需修改webxml的内容为其增加struts的FilterDispatcher修改后的webxml的内容如下

<?xml version= encoding=UTF?>

<webapp id=WebApp_ version= xmlns= xmlns:xsi=instance xsi:schemaLocation= app__xsd>

<displayname>Struts Blank</displayname>

<filter>

<filtername>struts</filtername>

<filterclass>orgapachestrutsdispatcherFilterDispatcher</filterclass>

</filter>

<filtermapping>

<filtername>struts</filtername>

<urlpattern>/*</urlpattern>

</filtermapping>

<welcomefilelist>

<welcomefile>l</welcomefile>

</welcomefilelist>

</webapp>

)建立源码目录和jsp存放目录

在src目录下建立example文件夹用于存放本实例的java文件等在WebRoot文件夹下建立子文件夹example用于存放本工程的jsp文件

编码

) Welcomejsp和Loginjsp的编写

首先我们建立Welcomejsp该文件包含两个链接点击登录链接后跳转到登录信息输入页面点击注册按钮跳转到注册页为了学习struts配置中的通配符使用我们暂不实现注册功能Welcomejsp的代码如下

<%@ page contentType=text/html; charset=UTF %>

<%@ taglib prefix=s uri=/strutstags %>

<html>

<head>

<title>欢迎界面</title>

<link <s:urlvalue=/css/examplecss/>rel=stylesheet type=text/css/>

</head>

<body>

<h>导航</h>

<ul>

<li><a <s:urlvalue=/example/Loginjsp/>>登录</a></li>

<li><a <s:urlaction=Register/>>注册</a></li>

</ul>

</body>

</html>

在该页面的顶部我们需要将struts的标签库引入语句为<%@ taglib prefix=s uri=/strutstags %>

在该页面主要用到struts的<s:url>标签该页面主要用到该标签的两个属性分别为value和action其中action属性表示用action来产生url而value表示使用的目标值在页面上点击查看源文件按钮可看到生成的语句分别变为

<link rel=stylesheet type=text/css/>

<a >

<a >

由此可知使用该标签时struts会自动为我们带上下文路径对于加了属性action的<s:url>标签后面会自动带上action作为后缀

点击登录链接后跳转到Loginjsp页面该页包含一个登录表单让用户输入用户名和密码信息用户点击提交按钮跳转到指定的Action——Login进行处理Loginjsp的内容如下

<%@ page contentType=text/html; charset=UTF %>

<%@ taglib prefix=s uri=/strutstags %>

<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>

该页用到Struts的表单标签<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>在此略

)配置文件strutsxml和examplexml

在上述jsp页面我们需跳转到两个Action地址需在struts的配置文件中配置因当工程变大时一个庞大的struts的配置极难维护建议按包路径分开配置文件所以本实例除了strutsxml配置文件外还新增了一个额外的配置文件examplexml该文件在strutsxml中引用strutsxml放在src目录下该文件的内容如下

<?xml version= encoding=UTF ?>

<!DOCTYPE struts PUBLIC

//Apache Software Foundation//DTD Struts Configuration //EN

dtd>

<struts>

<constant name=strutsenableDynamicMethodInvocation value=false />

<constant name=strutsdevMode value=false />

<include file=examplexml/>

<! Add packages here >

</struts>

可看到该文件通过<include file=examplexml/>将examplexml也作为struts的配置文件

接下来让我们看看examplexml的配置

<?xml version= encoding=UTF ?>

<!DOCTYPE struts PUBLIC

//Apache Software Foundation//DTD Struts Configuration //EN

dtd>

<struts>

<package name=example namespace=/example extends=strutsdefault>

<action name=Login_input method={} class=exampleLogin>

<result name=input>/example/Loginjsp</result>

<result type=redirectaction>Menu</result>

</action>

<! 为学习struts配置文件中通配符的使用我们将未定义的action的引用都定向到exampleExampleSupport这个Action中 >

<! 需定向的Action的名字传到{}中eg若请求Register这个action当ExampleSupport返回success时跳转到/example/Registerjsp >

<action name=* class=exampleExampleSupport>

<result>/example/{}jsp</result>

</action>

</package>

</struts>

) Login和ExampleSupport类以及验证配置类Loginvalidationxml的编写

在配置文件examplexml中定义了两个Action下面我们用代码来实现这两个Action

首先让我们来看看ExampleSupport这个Action这个Action不做任何操作集成自ActionSupport是本工程的各Action类的基类该类的代码如下

package example;

import comopensymphonyxworkActionSupport;

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 || valuelength() == );

}

private String username;

public String getUsername() {

return username;

}

public void setUsername(String username) {

thisusername = username;

}

private String password;

public String getPassword() {

return password;

}

public void setPassword(String password) {

thispassword = password;

}

}

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

<!DOCTYPE validators PUBLIC

//OpenSymphony Group//XWork Validator //EN

validatordtd>

<validators>

<field name=username>

<fieldvalidator type=requiredstring>

<message key=requiredstring/>

</fieldvalidator>

</field>

<field name=password>

<fieldvalidator type=requiredstring>

<message key=requiredstring/>

</fieldvalidator>

</field>

</validators>

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

)本地化输出——资源文件packageproperties

为了本地化的输出验证错误信息我们可以将参数信息和错误信息放入资源文件中资源文件packageproperties位于src/example目录下内容如下

requiredstring = ${getText(fieldName)}不能为空

password = 密码

username = 用户名

ssage = 该部分尚未构建请稍候访问

在src/example目下下建立对应的中文资源文件package_zh_CNproperties为了避免中文乱码问题我们编写一个批处理文件codebat来对packageproperties进行编码处理主要用到nativeascii命令其内容如下

del package_zh_CNproperties

copy packageproperties package_zh_CNpropertiesgbk

nativeascii encoding GBK package_zh_CNpropertiesgbk package_zh_CNproperties

del package_zh_CNpropertiesgbk

del *bak

运行该批处理文件可在package_zh_CNproperties中可看到编码后的资源文件信息如下

requiredstring = ${getText(fieldName)}\ued\ufd\uea\uaa

password = \ubc\u

username = \u\u\ud

ssage = \ube\ue\u\uca\ua\u\uefa\uffc\ubf\uad\u\ubbf\uee

)Registerjsp和Missingjsp的编写

中的examplexml中我们配置了通配符映射在Welcomejsp中我们使用

<a <s:url action=Register/>>注册</a>

其中的Register在example中找不到相关映射于是在用户点击注册按钮时将映射到通配符所映射的Actionexample ExampleSupport而后跳转到Registerjsp页面其代码如下

<%@ page contentType=text/html; charset=UTF %>

<%@ taglib prefix=s uri=/strutstags %>

<s:include value=Missingjsp/>

该页面包含Missingjsp页面其代码如下

<%@ page contentType=text/html; charset=UTF %>

<%@ taglib prefix=s uri=/strutstags %>

<html>

<head><title>未构建页面</title></head>

<body>

<p>

<! 读取配置文件中的对应信息 >

<s:text name=ssage/>

</p>

</body>

</html>

总结

到此为止我们的实例已构建完可通过浏览器访问看到我们的劳动成果下面总结一下我们在该实例中学到的东西

常用的一些标签的使用

在本实例中我们学习了<s:textfield><s:password><s:form>以及<s:url>的使用

表单数据的验证

本文描述了一个简单的表单的数据非空验证

本地化输出错误提示信息

在验证失败时用资源文件来输出错误提示信息

Strut配置文件中通配符的使用

对于某些地址我们可以使用在struts的配置通配符来使其映射到某个Action中去

上一篇:Eclipse工具使用格式化模板应用

下一篇:Hibernate+Spring搞定Clob、Blob的存取