我想使用Struts 2 Annotation来实现数据验证。
我的项目结构:
在Customer Model类中,我尝试使用:@RequiredStringValidator,但它失败了。 我的模型类
package com.dieu.customer.model;
import java.util.Date;
import javax.persistence.*;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
@Entity
@Table(name="test.customer")
public class Customer implements java.io.Serializable {
private Long customerId;
private String name;
private String address;
private Date createdDate;
public Customer() {
}
public Customer(String name, String ADDRESS, Date CREATED_DATE) {
this.name = name;
this.address = ADDRESS;
this.createdDate = CREATED_DATE;
}
@Id
@GeneratedValue
@Column(name="CUSTOMER_ID")
public Long getCustomerId() {
return this.customerId;
}
public void setCustomerId(Long CUSTOMER_ID) {
this.customerId = CUSTOMER_ID;
}
@Column(name="NAME")
public String getName() {
return this.name;
}
@RequiredStringValidator(type= ValidatorType.FIELD, message = "The name is required.")
public void setName(String NAME) {
this.name = NAME;
}
@Column(name="ADDRESS")
public String getAddress() {
return this.address;
}
@RequiredStringValidator(type= ValidatorType.FIELD, message = "The name is required.")
public void setAddress(String ADDRESS) {
this.address = ADDRESS;
}
@Column(name="CREATED_DATE")
public Date getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Date CREATED_DATE) {
this.createdDate = CREATED_DATE;
}
}
在CustomerAction类中,我尝试在Customer Add Action中使用@RequiredFieldValidator,但它也失败了。
我的动作类
package com.dieu.customer.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.dieu.customer.bo.CustomerBo;
import com.dieu.customer.model.Customer;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Namespace(value = "/")
public class CustomerAction implements ModelDriven{
Customer customer = new Customer();
List<Customer> customerList = new ArrayList<Customer>();
CustomerBo customerBo;
Customer editCustomer = new Customer();
public void setEditCustomer(Customer editCustomer) {
this.editCustomer = editCustomer;
}
public Customer getEditCustomer(){
return this.editCustomer;
}
//DI via Spring
public void setCustomerBo(CustomerBo customerBo) {
this.customerBo = customerBo;
}
public Object getModel() {
return customer;
}
public List<Customer> getCustomerList() {
return customerList;
}
public void setCustomerList(List<Customer> customerList) {
this.customerList = customerList;
}
//save customer
@Action(value = "/addCustomerAction", results = {@Result(name = "success", type="redirectAction", location = "listCustomerAction")})
@RequiredFieldValidator(type = ValidatorType.SIMPLE,
message = "Please specify your current job.",
fieldName = "name")
public String addCustomer() throws Exception{
//save it
customer.setCreatedDate(new Date());
customerBo.addCustomer(customer);
return "success";
}
//list all customers
@Action(value = "/listCustomerAction", results = {@Result(name = "success", location = "/pages/customer.jsp")})
public String listCustomer() throws Exception{
customerList = customerBo.listCustomer();
return "success";
}
@Action(value = "/deleteCustomerAction", results = {@Result(name = "success", type="redirectAction", location = "listCustomerAction")})
public String deleteCustomer() throws Exception{
HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
customerBo.deleteCustomer(Long.parseLong( request.getParameter("customerId")));
return "success";
}
@Action(value = "/getCustomerAction", results = {@Result(name = "success", location = "/pages/edit.jsp")})
public String editCustomer() throws Exception{
HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
editCustomer = customerBo.findCustomer(Long.parseLong( request.getParameter("customerId")));
return "success";
}
@Action(value = "/updateCustomerAction", results = {@Result(name = "success", type="redirectAction", location = "listCustomerAction")})
public String updateCustomer() throws Exception{
//save it
customer.setCreatedDate(new Date());
customerBo.addCustomer(customer);
return "success";
}
}
我的JSP:
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h2>Add Customer</h2>
<s:form action="addCustomerAction" >
<s:textfield name="name" label="Name" />
<s:textarea name="address" label="Address" cols="50" rows="5" />
<s:submit />
</s:form>
我的网页用户界面和客户列表
有人可以帮助我使用Struts 2 Annotion验证输入数据。