package com.model;
public class User {
private String username;
private int age;
public String getUsername () {
return username;
}
public void setUsername (String username) {
this .username = username;
}
public int getAge () {
return age;
}
public void setAge (int age) {
this .age = age;
}
}
package com.action;
import com.model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class Login extends ActionSupport implements ModelDriven <User >{
private User user=new User();
public User getUser() {
return user;
}
public void setUser(User user) {
this .user = user;
}
@Override
public User getModel() {
return user;
}
@Override
public String execute() throws Exception {
System.out.println(user.getUsername());
return super .execute();
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators >
<field name ="user.username" >
<field-validator type ="requiredstring" >
<message > 名字不能为空</message >
</field-validator >
</field >
<field name ="user.age" >
<field-validator type ="int" >
<param name ="min" > 18</param >
<param name ="max" > 60</param >
<message > 年龄必须在${min}到${max}之间</message >
</field-validator >
</field >
</validators >
<%--
Created by IntelliJ IDEA.
User: root
Date : 16 -9 -8
Time : 下午3 :10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html >
<head >
<title > Modelindex</title >
</head >
<body >
<s:form action ="login" method ="POST" >
<s:textfield name ="user.username" label ="帐号" > </s:textfield >
<s:textfield name ="user.age" label ="年龄" > </s:textfield >
<s:submit value ="提交" > </s:submit >
</s:form >
</body >
</html >