类型转换与输入校验总结
一, 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 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;}}
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;}}
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;}}