gpt4 book ai didi

java - Struts2 验证消息出现两次

转载 作者:太空宇宙 更新时间:2023-11-04 07:30:43 24 4
gpt4 key购买 nike

enter image description here

我正在尝试 struts 验证,但错误消息被打印两次。我的 Action 类如下:follow.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

最佳答案

这是因为您错误地使用了“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>

关于java - Struts2 验证消息出现两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17761439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com