Struts2验证消息出现两次

时间:2013-07-20 10:48:11

标签: java hibernate jsp configuration struts2

enter image description here

我正在尝试struts验证,但错误消息被打印两次。我的动作类如下.OSAction.java。我正在使用hibernate。我认为验证方法被调用两次,

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

在struts.xml中执行操作之前,只需实现interceptor-ref name =“defaultStack”