我正在尝试 struts 验证,但错误消息被打印了两次。我的动作类如下。OSAction.java。我在其中使用休眠。我认为 validate 方法被调用了两次,
package net.ajeet.os.view;
import java.util.List;
import net.ajeet.os.controller.OSManager;
import net.ajeet.os.model.OSDetail;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class OSAction extends ActionSupport {
private static final long serialVersionUID = 9149826260758390091L;
public OSDetail osdetail= new OSDetail();
private List<OSDetail> osdetails_list;
public OSDetail getOsdetail() {
return osdetail;
}
public void setOsdetail(OSDetail osdetail) {
this.osdetail = osdetail;
}
private Long id;
private OSManager linkController= new OSManager();
/* @Override
public OSDetail getModel() {
return osdetail;
}*/
/* public OSAction() {
linkController = new OSManager();
}
public String execute() {
this.osdetails_list = linkController.list();
return SUCCESS;
}
*/
public String add() {
try {
linkController.add(getOsdetail());
//linkController.add(osdetail);
} catch (Exception e) {
e.printStackTrace();
}
this.osdetails_list = linkController.list();
return SUCCESS;
}
/* public String delete() {
linkController.delete(getid());
return SUCCESS;
}*/
public List<OSDetail> getOsdetails_list() {
return osdetails_list;
}
public void setOsdetails_list(List<OSDetail> osdetails_list) {
this.osdetails_list = osdetails_list;
}
/* public Long getid() {
return id;
}
public void setid(Long id) {
this.id = id;
}
*/
public void validate()
{
if (osdetail.getOSname() == null || osdetail.getOSname().trim().equals(""))
{
addFieldError("osdetail.OSname","The OS Name is required");
}
if (osdetail.getOSversion() == null || osdetail.getOSversion().trim().equals(""))
{
addFieldError("osdetail.OSversion","The OS Version is required");
}
}
}
我的 Index.jsp 在下面
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>OS Manager - Struts2 Hibernate Example</title>
</head>
<body>
<h1>OS Manager</h1>
<s:actionerror/>
<s:form action="add" method="post" >
<s:hidden name="OSid" value="%{id}" />
<s:textfield name="osdetail.OSname" label="name" />
<s:textfield name="osdetail.OSversion" label="version"/>
<s:textfield name="osdetail.OSnotes" label="notes"/>
<s:submit value="Add OS Details" align="center"/>
<s:reset value="Reset" />
</s:form>
<h2>OS Details</h2>
<table>
<tr>
<th>OS Name</th>
<th>OS Version</th>
<th>OS Notes</th>
</tr>
<s:iterator value="osdetails_list" var="osdetail">
<tr>
<td><s:property value="OSname"/></td>
<td><s:property value="OSversion"/></td>
<td><s:property value="OSnotes"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
Struts.xml 如下
![<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<package name="default" extends="struts-default" namespace="/">
<action name="add"
class="net.ajeet.os.view.OSAction" method="add">
<result name="success" type="chain">index</result>
<result name="input" type="chain">index</result>
</action>
<action name="index"
class="net.ajeet.os.view.OSAction">
<result name="success">index.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>][2]
enter code here