Struts2学习笔记6:Struts2输入检验
第五讲
在MyEclipse6.5的快捷键中把我们习惯性使用的Alt+/进行代码自动补齐的快捷键改为了ctrl+空格,大家知道这是我们切换中英文输入法的键,所以我们需要更改这个快捷键,方法如下: 1.选择MyEclipse6.5菜单栏中的Window->preferences; 2.选择General->keys; 3.在右侧中间的窗体中点击word completion后再点击remove binding; 4.然后选择Content Assist点击Remove binding,在binding中输入Alt+/; 5.点击Ok就可以了。
|
Struts7 制作一个表单,对表单中的内容进行自定义验证。
1.新建版本 struts7,tomcat中server.xml文件中声明。
2.新建register.jsp文件,用以填写登记信息,贴入代码以下:
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=GB18030">
<title>用户信息登记表</title>
</head>
<body>
<s:fielderror></s:fielderror>
<form id="form1" name="form1" method="post"
action="register.action">
<table width="400" border="1" align="center">
<tr>
<td>
用户名:
</td>
<td>
<label>
<input type="text" name="username" />输入长度6-10
</label>
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<label>
<input type="password" name="password" />输入长度6-10
</label>
</td>
</tr>
<tr>
<td>
确认密码:
</td>
<td>
<label>
<input type="password" name="re_password" />输入长度6-10
</label>
</td>
</tr>
<tr>
<td>
年龄:
</td>
<td>
<label>
<input type="text" name="age" />输入年龄1-120
</label>
</td>
</tr>
<tr>
<td>
出生日期:
</td>
<td>
<label>
<input type="text" name="birthday" />
</label>
</td>
</tr>
<tr>
<td>
毕业日期:
</td>
<td>
<label>
<input type="text" name="graduation" />
</label>
</td>
</tr>
<tr>
<td>
<label>
<input type="submit" name="submit" value="提交" />
</label>
</td>
<td>
<label>
<input type="reset" name="reset" value="重置" />
</label>
</td>
</tr>
</table>
</form>
</body>
</html>
说明:需要注意的地方已经做了标记。代码简单,不详加说明。
3.新建success.jsp文件用于返回信息,贴入以下代码:
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>返回登记信息</title>
</head>
<body>
<table width="400" border="1" align="center">
<tr>
<td>
用户名:
</td>
<td>
<label>
${requestScope.username }
</label>
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<label>
${requestScope.password }
</label>
</td>
</tr>
<tr>
<td>
年龄:
</td>
<td>
<label>
${requestScope.age }
</label>
</td>
</tr>
<tr>
<td>
出生日期:
</td>
<td>
<label>
${requestScope.birthday }
</label>
</td>
</tr>
<tr>
<td>
毕业日期:
</td>
<td>
<label>
${requestScope.graduation }
</label>
</td>
</tr>
</table>
</body>
</html>
说明:需要注意的地方已经做了标记。代码简单,不详加说明。
4.新建action.RegisterAction.java文件,用以进行验证处理。继承
ActionSupport类
package action;
import java.util.Calendar;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private static final long serialVersionUID = 3379963077373980301L;
private String username;
private String password;
private String re_password;
private int age;
private Date birthday;
private Date graduation;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRe_password() {
return re_password;
}
public void setRe_password(String re_password) {
this.re_password = re_password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getGraduation() {
return graduation;
}
public void setGraduation(Date graduation) {
this.graduation = graduation;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
@Override
public void validate() {
//下面几行代码的作用
//1.对输入的内容进行验证。
//2.可以防止直接在浏览器中贴入跳转页面的URL地址
if( null == this.username || "".equals(this.username.trim()) )
{
this.addFieldError( "username" , "用户名不能为空。" );
}
if ( null == this.password || "".equals(this.password.trim()) ){
this.addFieldError( "password" , "密码不能为空。" );
}
if ( null == this.re_password || "".equals(this.re_password.trim()) ){
this.addFieldError( "re_password" , "确认密码不能为空。" );
}
if ( 0 == this.age ){
this.addFieldError( "age" , "年龄不能为空或0。" );
}
if ( null == this.birthday ){
this.addFieldError( "birthday" , "出生日期不能为空。" );
}
if ( null == this.graduation ){
this.addFieldError( "graduation" , "毕业日期不能为空。" );
}
if ( 0 != this.age ){
if ( this.age < 1 ){
this.addFieldError( "age", "年龄太小了。" );
} else if ( this.age > 120 ){
this.addFieldError( "age", "年龄太大了。" );
}
}
if ( null!=this.username && !"".equals(this.username.trim()) ){
if ( this.username.length() < 6 ){
this.addFieldError( "username", "用户名长度必须大于6。");
} else if ( this.username.length() > 10 ){
this.addFieldError( "username", "用户名长度必须小于10。" );
}
}
if ( null!=this.password && !"".equals(this.password.trim()) ){
if ( this.password.length() < 6){
this.addFieldError( "password", "密码长度必须大于6。");
} else if ( this.username.length() > 10 ){
this.addFieldError( "password", "密码长度必须小于10。" );
} else if ( !this.password.equals(this.re_password) ) {
this.addFieldError( "password", "密码与确认密码输入不一致。" );
}
}
if ( null!=this.birthday && null!=this.graduation ){
Calendar c1 = Calendar.getInstance();
c1.setTime( this.birthday );
Calendar c2 = Calendar.getInstance();
c2.setTime( this.graduation );
if ( c1.after(c2) ){
this.addFieldError( "birthday" , "毕业日期怎么比出生日期还早呢?" );
}
}
}
}
说明:都是一些条件判断语句,看一眼就明白了,不多加解释。
5.修改struts.xml文件添加一个action标签
<action name="register" class="action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register.jsp</result>
</action>