原创作者: radovi   阅读:3969次   评论:0条   更新时间:2011-05-26    
注:本文系作者在看了浪曦的风中叶老师的struts2视频的个人总结,希望能帮助广大struts2的初学者。

第一步:
(这一步和其他一样,这里从简)依旧是新建一个web project,命名为shuruxiaoayn,导入struts2必须的包。在src目录下新建struts.xml,修改web.xml文件。

第二步:
将index.jsp改名为regt.jsp(这个不是必须的,事实上也没有必要,此处只是为了便于称呼)。reg.jap的代码如下

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s"  uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</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">
	-->
  </head>
  
  <body>
			<s:form action="reg" >
					<s:textfield  name="username" label="username"></s:textfield>
					<s:password name="password" label="password"></s:password>
					<s:password name="repassword" label="repassword"></s:password>
					<s:textfield name="age" label="age"></s:textfield>
					<s:textfield name="birthday" label="birthday"></s:textfield>
					<s:textfield name="graduation" label="graduation"></s:textfield>
					
					<s:submit name="submit"></s:submit>
					<s:reset name="reset"></s:reset>
			</s:form>

  </body>
</html>


第二步:action
在src目录下新建新建包com.action 在其中新建一个RegAction.java文件
代码如下
package com.action;

import java.util.Calendar;
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class RegAction  extends ActionSupport

{
		private String username;
		private String password;
		private String repassword;
		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 getRepassword() {
			return repassword;
		}
		public void setRepassword(String repassword) {
			this.repassword = repassword;
		}
		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;
		}
		
		public String execute() throws Exception
		{
			return 	SUCCESS;
		}
		
		public void validate()
		{
			System.out.println("已经调用了效验方法");
			if(null == username || username.length() <6 || username.length() >10)
			{
				this.addFieldError("username","username needs 6 to 10 chars");
			}
			if(null == password || password.length() <6 || password.length() >10)
			{
				this.addFieldError("password", "password needs 6 to 10 chars");
			}
			if(null == repassword || repassword.length() <6 || repassword.length() >10)
			{
				this.addFieldError("repassword", "repassword needs 6 to 10 chars");
			}
			if( null != password || null !=repassword)
			{
				 if(  !password.equals(repassword) )
				 {
					 this.addFieldError("repassword", "two pasword should be same");
				 }
			}
			
			if(age <=0 || age >150)
			{
				this.addFieldError("age", "age should between 1 to 149");
			}
			if(null == birthday )
			{
				this.addFieldError("birthday", "birthday required");
			}
			if(null == graduation)
			{
				this.addFieldError("graduation", "graduation required");
			}
			if(null != birthday && null != graduation)
			{
				  Calendar c1 =Calendar.getInstance();
				   c1.setTime(birthday);
				   
				   Calendar c2 = Calendar.getInstance();
				   c2.setTime(graduation);
				   
				   if( !c1.before(c2))
				   {
					   this.addFieldError("birthday", "birthday should be after the graduation");
				   }
					   
			}
			
			
		}
		
		
		
		
		
}




第四步:
在WebRoot目录下新建另一个视图 success.jsp (此文件在这个效验中基本没有什么作用)

第五步:修改struts.xml文件 代码惹下

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts PUBLIC
 	"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
 	"struts.apache.org/dtds/struts-2.0.dtd">
<struts>
		<package name="shuruxiaoyan" extends="struts-default">
				<action name="reg" class="com.action.RegAction">
						<result name="success">/success.jsp</result>
						<result name="input">/reg.jsp</result>
				</action>
		</package>

</struts>


ok,到此,已经完成了最最基本的输入校验工作;
运行界面会是如下:
图1





图2




显然。上面中图2中有些信息不是我们制定的,这是由于输入的数据没有通过类型转换造成的;
当然,如果想让出错提示变得更友好些,在进行如下操作:

第六步:配置属性文件
在对应的action目录中新建一个RegAction.properties文件 过程如下

1.新建这个文件
2.打开jdk安装目录下的native2ascii应用程序 路径一般类似 作者的目录是
O:\Program Files\Java\jdk1.6.0_10\bin\native2ascii
在这个环境中,输入以下三行 分别按回车

invalid.fieldvalue.age=年龄格式有问题或类型转化错误
invalid.fieldvalue.birthday=出生日期格式有问题或类型转换错误
invalid.fieldvalue.graduation=毕业日期格式有问题或类型转换错误

会出现三行对应的Unicode代码 放入刚刚建立的属性文件即可:
附图









好了 现在是弄好了较简单的输入校验了
预览一下:
当填写如下表单时:




完成后


  • 描述: 这是符合要求的
  • 大小: 7.2 KB
  • 大小: 8.1 KB
  • 大小: 12.5 KB
  • 大小: 27.9 KB
  • 大小: 6.1 KB
  • 大小: 10.4 KB
  • 大小: 10.5 KB
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

  • radovi在2009-02-23创建
  • radovi在2011-05-26更新
  • 标签: 输入校验, 表单提交
Global site tag (gtag.js) - Google Analytics