package com.test.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport
{
public InputStream getDownloadFile()
{
return ServletActionContext.getServletContext().getResourceAsStream(
"/upload/Struts2.ppt");
}
@Override
public String execute() throws Exception
{
return SUCCESS;
}
}
package com.test.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
private String username;
private String password;
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 execute() throws Exception
{
if ("hello".equals(this.getUsername().trim())
&& "world".equals(this.getPassword().trim()))
{
return "success";
}
else
{
this.addFieldError("username", "username or password error");
return "failer";
}
}
@Override
public void validate()
{
if (null == this.getUsername() || "".equals(this.getUsername().trim()))
{
this.addFieldError("username", "username required");
}
if (null == this.getPassword() || "".equals(this.getPassword().trim()))
{
this.addFieldError("password", "password required");
}
}
}
package com.test.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.Point;
public class PointAction extends ActionSupport
{
//private List<Point> point;
private Point point;
// private Point point2;
// private Point point3;
private int age;
private String username;
private Date date;
public Point getPoint()
{
return point;
}
public void setPoint(Point point)
{
this.point = point;
}
// public Point getPoint2()
// {
// return point2;
// }
//
// public void setPoint2(Point point2)
// {
// this.point2 = point2;
// }
//
// public Point getPoint3()
// {
// return point3;
// }
//
// public void setPoint3(Point point3)
// {
// this.point3 = point3;
// }
// public List<Point> getPoint()
// {
// return point;
// }
//
// public void setPoint(List<Point> point)
// {
// this.point = point;
// }
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
@Override
public String execute() throws Exception
{
return SUCCESS;
}
}
package com.test.action;
import java.util.Calendar;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction 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;
}
@Override
public String execute() throws Exception
{
return SUCCESS;
}
public String test() throws Exception
{
return SUCCESS;
}
// public String abc() throws Exception
// {
// System.out.println("abc method invoked");
//
// return SUCCESS;
// }
//
// public void validateAbc()
// {
// System.out.println("validateAbc() invoked");
// }
//
@Override
public void validate()
{
System.out.println("validate~~~~~~~~~~~~~~~~~~~");
this.addFieldError("username","aaaaaaaaaaaaaaaa");
this.getFieldErrors().put("username","bbbbbbbbbbbbb");
if (null == username || username.length() < 6 || username.length() > 10)
{
this.addActionError("username invalid");
}
// if (null == password || password.length() < 6 || password.length() > 10)
// {
// this.addActionError("password invalid");
// }
// else if (null == repassword || repassword.length() < 6
// || repassword.length() > 10)
// {
// this.addActionError("re-password invalid");
// }
// else if (!password.equals(repassword))
// {
// this.addActionError("two passwords not the same");
// }
// if (age <= 0 || age > 150)
// {
// this.addActionError("age should be between 1 and 150");
// }
//
// if (null != birthday && null != graduation)
// {
// Calendar c1 = Calendar.getInstance();
// c1.setTime(birthday);
//
// Calendar c2 = Calendar.getInstance();
// c2.setTime(graduation);
//
// if (!c1.before(c2))
// {
// this.addActionError("birthday should be before graduation");
// }
// }
}
}
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();
public User getModel()
{
return user;
}
public void prepare() throws Exception
{
System.out.println("hello world");
}
@Override
public String execute() throws Exception
{
return SUCCESS;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<!--
<validator type="requiredstring">
<param name="fieldName">username</param>
<message>username should not be blank!</message>
</validator>
<validator type="stringlength">
<param name="fieldName">username</param>
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>username should be between ${minLength} and ${maxLength}</message>
</validator>
-->
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>username should not be blank!</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>username should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>password should not be blank!</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>password should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<field name="repassword">
<field-validator type="requiredstring">
<message>repassword should not be blank!</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>repassword should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
<field name="age">
<field-validator type="required">
<message>age should not be blank!</message>
</field-validator>
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>age should be between ${min} and ${max}</message>
</field-validator>
</field>
<field name="birthday">
<field-validator type="required">
<message>birthday should not be blank!</message>
</field-validator>
<field-validator type="date">
<param name="min">2001-01-01</param>
<param name="max">2003-12-31</param>
<message>birthday should be between ${min} and ${max}</message>
</field-validator>
</field>
<field name="graduation">
<field-validator type="required">
<message>graduation should not be blank!</message>
</field-validator>
<field-validator type="date">
<param name="min">2005-01-01</param>
<param name="max">2007-12-31</param>
<message>graduation should be between ${min} and ${max}</message>
</field-validator>
</field>
</validators>
package_en_US.properties
username.xml.invalid = package information
PointAction-conversion.properties
#point=com.test.converter.PointConverter
#point2=com.test.converter.PointConverter
#point3=com.test.converter.PointConverter
#point=com.test.converter.PointConverter3
RegisterAction_en_US.properties
username.xml.invalid = RegisterAction information