首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

类型转换与输入校验小结

2012-08-27 
类型转换与输入校验总结一, struts2标签可以保留输入信息1.register2.jsp注:可以不用s:form 标签的默认

类型转换与输入校验总结
一, struts2标签可以保留输入信息
1.register2.jsp
注:可以不用<s:form >标签的默认布局,如<s:form theme=”simple”>就变成普通的html标签了

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %><!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=UTF-8"><title>Insert title here</title></head><body><s:form action="register"><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="graduaction" label="graduaction"></s:textfield><s:submit value="submit"></s:submit></s:form></body></html>

二,Action中属性驱动(PropertyDriven)和模型驱动(ModelDriven)

1.属性驱动(PropertyDriven)就是Action中的成员变量
2.模型驱动(ModelDriven)
2.1register2.jsp
2.2struts.xml
<action name="register" name="code">package com.test.action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.test.bean.User;public class RegisterAction2 extends ActionSupport implements ModelDriven<User> {private User user=new User();@Overridepublic User getModel() {return user;}@Overridepublic String execute() throws Exception {return SUCCESS;}}

2.4User.java
package com.test.bean;import java.util.Date;public class User {private String username;private String password;private String repassword;private int age;private Date birthday;private Date graduaction;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 getGraduaction() {return graduaction;}public void setGraduaction(Date graduaction) {this.graduaction = graduaction;}}

三,Action中执行execute()之前准备工作的单独实现
1.RegisterAction2.java
package com.test.action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.Preparable;import com.test.bean.User;public class RegisterAction2 extends ActionSupport implements ModelDriven<User>,Preparable {private User user=new User();@Overridepublic User getModel() {return user;}@Overridepublic void prepare() throws Exception {System.out.println("prepare");}@Overridepublic String execute() throws Exception {System.out.println("execute");return SUCCESS;}}

注:执行顺序是prepare方法最先执行,再执行类型转换,输入校验,execute()




热点排行