注:本文系做者在看了浪曦的风中叶老师的struts2视频的我的总结,但愿能帮助广大struts2的初学者。
第一步:
(这一步和其余同样,这里从简)依旧是新建一个web project,命名为shuruxiaoayn,导入struts2必须的包。在src目录下新建struts.xml,修改web.xml文件。
第二步:
将index.jsp更名为regt.jsp(这个不是必须的,事实上也没有必要,此处只是为了便于称呼)。reg.jap的代码以下
html
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> web
<% apache
String path = request.getContextPath(); 框架
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; jsp
%> ui
<%@ taglib prefix="s" uri="/struts-tags"%> this
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> spa
<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代码 放入刚刚创建的属性文件便可:
附图
好了 如今是弄好了较简单的输入校验了
预览一下:
当填写以下表单时:
完成后
struts2的类型转换——Struts2第二讲 | Struts2输入校验2(框架效验)———stru ...
shuruxiaoyan发布文件.rar (3.2 MB)
下载次数: 75
shuruxiaoyan开发文件.rar (3.2 MB)
下载次数: 65