使用验证框架进行校验JSP页面元素:
创建验证文件:ActionName-validation.xml
使用验证框架,编写验证规则
反编译工具的配置:可以看jar包里写的类的源代码。
1、E:\Program Files (x86)\Java\jdk1.7.0_07\bin 放置jad.exe文件
2、E:\Program Files (x86)\myeclipse10\MyEclipse 10\dropins 放置net.sf.jadclipse_3.3.0.jar文件
重新打开myeclipse,就OL。
Alt + left (<--) 左返回
Alt + right (-->)右返回
Strtuts2 国际化学习:
1、Struts.xml:设置基名
<constant name="struts.custom.i18n.resources" value="aaa"></constant>
2、创建 : 以 基名_en.properties / 基名_zh_CN.properties
测试:浏览器-》工具-》语言(中、英)
以下是具体代码演示:
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Struts2 验证框架学习</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function _onCg(val)
{
window.location.href = "defAction?request_locale="+val;
}
</script>
</head>
<body>
<s:form action="usManager" >
<!-- s:标签属性不再是label而改成key,当提交的时候,它会自动通过配置好的基名去和对应的:基名_en.properties / 基名_zh_CN.properties文件中的类容进行匹配 -->
<s:textfield name="userInfo.usName" key="usName"></s:textfield>
<s:password name="userInfo.usPass" key="usPass"></s:password>
<s:password name="rePass" key="rePass"></s:password>
<s:textfield name="userInfo.phone" key="phone"></s:textfield>
<s:textfield name="time" key="time"></s:textfield>
<s:submit value="%{getText('sub')}"></s:submit>
<s:select list="#{'en':'英文','zh_CN':'中文'}" name="locale" οnchange="_onCg(this.value)"></s:select>
</s:form>
<s:property value="#attr.locale"/>
</body>
</html>
success.jsp:
<body>
Success<br>
</body>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="aaa"></constant>
<package name="TestManager" extends="struts-default">
<action name="usManager" class="com.zuxia.yc42.action.UserManagerAction">
<result name="success">/jsp/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
<action name="defAction">
<result>/index.jsp</result>
</action>
</package>
</struts>
UserInfo:
package com.zuxia.yc42.po;
import java.io.Serializable;
public class UserInfo implements Serializable
{
private String usName;
private String usPass;
private String phone;
public String getUsName() {
return usName;
}
public void setUsName(String usName) {
this.usName = usName;
}
public String getUsPass() {
return usPass;
}
public void setUsPass(String usPass) {
this.usPass = usPass;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
UserManagerAction:
package com.zuxia.yc42.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
import com.zuxia.yc42.po.UserInfo;
public class UserManagerAction extends ActionSupport
{
private UserInfo userInfo;
private String rePass;
private Date time;
public String test()
{
return SUCCESS;
}
public UserInfo getUserInfo() {
return userInfo;
}
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
public String getRePass() {
return rePass;
}
public void setRePass(String rePass) {
this.rePass = rePass;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
}
UserManagerAction-validation.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!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="userInfo.usName">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户名不允许为空</message>
</field-validator>
</field>
<field name="userInfo.usPass">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>密码不允许为空</message>
</field-validator>
<field-validator type="stringlength">
<param name="maxlength">10</param>
<param name="minlength">5</param>
<message>密码只能在${minlength}跟${maxlength}之间</message>
</field-validator>
</field>
<field name="rePass">
<field-validator type="fieldexpression">
<param name="expression">
userInfo.usPass == rePass
</param>
<message>两次密码输入有误</message>
</field-validator>
</field>
<field name="userInfo.phone">
<field-validator type="regex">
<param name="expression">^(\d{3,4}-){0,1}(\d{7,8})$</param>
<message>座机格式不正确</message>
</field-validator>
</field>
</validators>
aaa_en.properties:
usName=User Name
usPass=User Password
rePass=Request Password
phone=Phone
time=Time
sub=Submit
aaa_zh_CN.properties:
aaa.properties: