WebWork实现用户的登陆收藏
新一篇: J2SE IO 流复习 | 旧一篇: Struts+Hibernate+Spring 集成
<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>主要实现如下功能:1、login.jsp中就name,password字段
2、用户名与密码不能为空!如果空给予提示(使用Webwork验证,国际化)
2、当用户名为:xiaomaha密码为123跳转到seccess.jsp页面否则跳转到defeat.jsp页面
第一步: 导包
版本为: webwork-2.2.5
http://www.opensymphony.com/webwork 可以下载所需要的包
1、webwork-2.2.5.jar
2、lib目录下default中的所有包
第二步:配置web.xml
com.opensymphony.webwork.dispatcher.FilterDispatcher 为WebWork中的Servlet控制器
<?
xml version="1.0" encoding="UTF-8"
?>
< web-app version ="2.4"
xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< filter >
< filter-name > webwork </ filter-name >
< filter-class >
com.opensymphony.webwork.dispatcher.FilterDispatcher
</ filter-class >
</ filter >
< filter-mapping >
< filter-name > webwork </ filter-name >
< url-pattern > *.action </ url-pattern >
</ filter-mapping >
</ web-app >
< web-app version ="2.4"
xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< filter >
< filter-name > webwork </ filter-name >
< filter-class >
com.opensymphony.webwork.dispatcher.FilterDispatcher
</ filter-class >
</ filter >
< filter-mapping >
< filter-name > webwork </ filter-name >
< url-pattern > *.action </ url-pattern >
</ filter-mapping >
</ web-app >
Filter是一个过滤器,页面以*.action的请求都交给WebWork的servlet控制器
第三步: 创建Action
首先创建一个Action,WebWork中Action其实就是一个普通的JAVA类。只要写一个execute()方法 返回
String类型,在配置文件定义它.那么它就是一个Action。相当与Struts1中的Action!WebWork实现了与Servlet解偶合,方便测试`本人特别喜欢!!
但是如果需要方便的开发那么继承ActionSupport,覆写它的execute()注意它的返回值是一个String,不是一个ActionForword
WebWork中可以使用两中方法实现验证
1、在Action中继承ActionSupport,覆写validate()方法(本人给予注释了!)
2、配置文件实现,本例使用配置文件, 注意必须将验证的配置文件写到与自定义Action同包下!
package
com.xiaomaha.action;
import com.opensymphony.xwork.ActionSupport;
public class LoginAction extends ActionSupport {
private String userName;
private String passWord;
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String execute() throws Exception {
String path = null ;
if("xiaomaha".equalsIgnoreCase(this.userName)&&"123".equals(this.passWord)){
path =this.SUCCESS;
}else{
path =this.ERROR;
}
return path;
}
// public void validate() {
// if("".equals(this.userName)||this.userName==null){
// /*addFieldError()相当与Struts1中的ActionErrors*
// * 如果要实现国际化那么需要getText()它用于读取资源文件
// * getText("name.null")对应资源文件中的key,它会找到value
// */
// this.addFieldError("userName", getText("name.null"));
// }
//
// if("".equals(this.passWord)||this.passWord==null){
// this.addFieldError("passWord", getText("pass.null"));
// }
// }
}
import com.opensymphony.xwork.ActionSupport;
public class LoginAction extends ActionSupport {
private String userName;
private String passWord;
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String execute() throws Exception {
String path = null ;
if("xiaomaha".equalsIgnoreCase(this.userName)&&"123".equals(this.passWord)){
path =this.SUCCESS;
}else{
path =this.ERROR;
}
return path;
}
// public void validate() {
// if("".equals(this.userName)||this.userName==null){
// /*addFieldError()相当与Struts1中的ActionErrors*
// * 如果要实现国际化那么需要getText()它用于读取资源文件
// * getText("name.null")对应资源文件中的key,它会找到value
// */
// this.addFieldError("userName", getText("name.null"));
// }
//
// if("".equals(this.passWord)||this.passWord==null){
// this.addFieldError("passWord", getText("pass.null"));
// }
// }
}
第四步: 创建xwork.xml
xwork.xml有点像Struts1中的struts-config.xml
xwork.xml文件必须放在类路径下 WEB-INF/classes/ 之下
WebWork的Servlet控制器会去找到它并解析它,路径一定不要放错了,名字一定不要错!
还要提示注意,在WebWork中所有的Action标签必须在package标签下,package是action的父标签
<include file="webwork-default.xml"></include>会去找到WebWork中JAR文件中的webwork-default.xml,必写
<!
DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd"
>
< xwork >
<!-- include加载webwork中的webwork-default.xml是必须的不然会抛出
servlet jsp threw exception
-->
< include file ="webwork-default.xml" ></ include >
< package name ="webworkdemo" extends ="webwork-default" >
< action name ="login" class ="com.xiaomaha.action.LoginAction" >
< result name ="success" > /success.jsp </ result >
< result name ="error" > /error.jsp </ result >
< result name ="input" > /login.jsp </ result >
</ action >
</ package >
</ xwork >
< xwork >
<!-- include加载webwork中的webwork-default.xml是必须的不然会抛出
servlet jsp threw exception
-->
< include file ="webwork-default.xml" ></ include >
< package name ="webworkdemo" extends ="webwork-default" >
< action name ="login" class ="com.xiaomaha.action.LoginAction" >
< result name ="success" > /success.jsp </ result >
< result name ="error" > /error.jsp </ result >
< result name ="input" > /login.jsp </ result >
</ action >
</ package >
</ xwork >
<result name="success">/success.jsp</result> 如果Action中的execute()方法返回success字符串那么,跳转到success.jsp页面中
<result name="input">/login.jsp</result> 和Struts1中的input一样都是显示错误消息,但在WebWork中不需要定义返回错误的标签。因为WebWork中定义的标签 中<w:form>已经实现了错误验证
第五步: 验证配置文件
在自定义的Action 同包下建立一个与Action一样名字+validation.xml文件
例如: Action:LoginAction.java
配置文件:LoginAction-validation.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd" >
< validators >
< field name ="userName" >
< field-validator type ="requiredstring" >
< message key ="name.null" ></ message >
</ field-validator >
</ field >
< field name ="passWord" >
< field-validator type ="requiredstring" >
< message key ="pass.null" ></ message >
</ field-validator >
</ field >
</ validators >
<! DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd" >
< validators >
< field name ="userName" >
< field-validator type ="requiredstring" >
< message key ="name.null" ></ message >
</ field-validator >
</ field >
< field name ="passWord" >
< field-validator type ="requiredstring" >
< message key ="pass.null" ></ message >
</ field-validator >
</ field >
</ validators >
requiredstring 代表:不能为空,是必须的
第六步:创建国际化资源文件
创建一个Message.properties ,里面不能写中文。
需要在dos窗口中进行转码
命令为native2ascii Message.properties Message.properties_zh_cn.properties 就生成了一个新的文件,删除Message.properties
浏览器如果为中文,那么它会找到 Message.properties_zh_cn.properties ,为了方便我就不定义其它语言的配置文件咯!
提示:Message 可以任意修改 !
例:比如需要定义支持英语的可以这样写 Message_en_US.properties
Message.properties_zh_cn.properties如下:
name.null = /u7528/u6237/u540d/u4e0d/u80fd/u4e3a/u7a7a/uff01
pass.null = /u5bc6/u7801/u4e0d/u80fd/u4e3a/u7a7a/uff01
pass.null = /u5bc6/u7801/u4e0d/u80fd/u4e3a/u7a7a/uff01
最后一步:加载上面的资源文件
在类路径下创建 webwork.properties ,WebWork会在类路径找到它并且解析它!
里面添加如下:
webwork.custom.i18n.resources=Message
Ok一切搞定咯