1

在此处输入图像描述

我正在尝试 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
4

2 回答 2

1

这是因为您错误地使用了"chain"结果。除非您知道自己在做什么,否则不要使用它。要修复错误,您应该像这样更改配置

<action name="add" class="net.ajeet.os.view.OSAction" method="add">
  <result type="redirectAction">index</result>
  <result name="input">/index.jsp</result>
</action>

<action name="index" class="net.ajeet.os.view.OSAction">
  <result>/index.jsp</result>
</action>
于 2013-07-20T11:14:38.347 回答
0

只需在 struts.xml 中的操作之前实现interceptor-ref name="defaultStack"

于 2013-10-18T23:37:27.277 回答