【问题标题】:How to post error messages to the HTML from Struts2 outside of Action class如何在 Action 类之外从 Struts2 将错误消息发布到 HTML
【发布时间】:2012-11-13 08:18:34
【问题描述】:

我有一个注册程序。当我在数据库中插入一条记录时,我将实例化一个类并调用方法insert()。当我插入相同的记录时,当然会出现重复数据错误和大量错误消息。我想用trycatch 捕获它。我能做到。但是,我不知道如何向 JSP 显示消息。

据我所知,在动作类中,validate() 方法和validation.xml 首先运行。调用这些方法后发生插入重复错误。

import com.opensymphony.xwork2.ActionSupport;
import lotmovement.business.crud.InsertUserProfile;
import lotmovement.business.entity.UserProfile;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RegisterAction extends ActionSupport {

    private static String userId;
    private static String password;
    private static String firstName;
    private static String lastName;
    private static int securityLevel;

    @Override
     public String execute() {    
         ApplicationContext context = 
                 new ClassPathXmlApplicationContext("spring.xml");

          InsertUserProfile iup = 
                 (InsertUserProfile)context.getBean("insertuserprofile");
         iup.Insert();       

      return SUCCESS;
    }

这是我的插入用户配置文件方法

 public void Insert() {          
    ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    UserProfile up = (UserProfile)context.getBean("userprofile");
    RegisterAction ra = (RegisterAction)context.getBean("registeraction");
    EntityStart es = (EntityStart)context.getBean("entitystart");

    es.StartDbaseConnection();

    up.setUserId(ra.getUserId());
    up.setFirstName(ra.getFirstName());
    up.setLastName(ra.getLastName());
    up.setPassword(ra.getPassword());
    up.setSecurityLevel(ra.getSecurityLevel());

    es.StartPopulateTransaction(up);

    es.CloseDbaseConnection();        
}

这是我的 JSP:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <link rel="stylesheet" type="text/css" href="CSS/register.css">
  <title>Register</title>
  <s:head />
</head>
<body>
<s:form method="POST" action="register" >
  </tr>
  <tr>
    <td colspan="2">
      <s:actionerror/>
      <s:fielderror/>
    </td>
  </tr>
  <s:textfield label="UserID"     key="userId" maxLength="20"/>
  <s:password label="Password"    key="password" maxLength="20"/>
  <s:password label="retype-Password"  key="retypepassword" maxLength="20"/>
  <s:textfield label="Firstname"  key="firstName" maxLength="20"/>
  <s:textfield label="Lastname"   key="lastName" maxLength="20"/>
  <s:textfield label="SecurityLevel" key="securityLevel" maxLength="20"/>
  <s:submit   value="Register"/>
</s:form>
</body>
</html>

【问题讨论】:

  • 请考虑使用S2 Spring插件;你在你的行动中做了很多不必要和低效的手工工作。此外,动作是按请求实例化的:静态变量在几乎所有情况下都非常糟糕,会导致竞争条件和其他不确定的行为,负载下的特殊性。

标签: java spring validation jsp struts2


【解决方案1】:

ActionSupport 中有 addActionErroraddFieldError 方法。您可以在 validate 方法中捕获任何错误。如果提交了错误的数据,则调用这些方法。应用后,请求将分派到input 结果。在 JSP 中你可以使用 &lt;s:actionerror&lt;s:fielderror 显示您通过上述方法添加的错误。

【讨论】:

  • 你的意思是即使 ActionSupport 在动作类之外你也可以使用它?如果我在我的 insertrecord 类上放置一个验证方法,验证方法仍然是第一个触发的方法吗?
  • 不,您的操作应该扩展ActionSupport,它具有覆盖validate 方法。
  • 是的,这是我的问题,因为我所做的是。动作类---> 插入记录类。错误发生在插入记录类的插入方法中。 insertrecord 已经在 actionclass 之外了?对不起,我的愚蠢问题是这方面的初学者。谢谢
  • 您可以编写一个代码来检查您是否可以插入记录,然后从validate 方法中调用此代码。如果结果是否定的,那么就按照我的回答去做。
  • 非常感谢罗曼。所以 try catch 不应该在 insert 方法里面。而是在动作课上。 :)
【解决方案2】:

试试这个:

1.定义自己的异常类

public class MyException extends Exception {}

2.抛出这个异常,如果你的insert方法有异常

if(insertError){
    throw new MyExecption("your message");
}

3.在action类中,调用insert method with try-catch block

try{
    insert();
} catch(MyException me){
    addActionError(me.getMessage());
    return "anything_you_want";
}

4.你可以使用&lt;s:actionerror/&gt;在jsp中显示消息

【讨论】:

  • 这是最糟糕的解决方案。
  • 看我的回答,cmets中有一个链接可以很好的解释文章。
  • @RomanC 我读了你的文章,在这种情况下,他在进行数据库访问之前无法验证,如果你坚持重写validate 方法,他每次填充操作时都必须访问数据库 2 次。一次检查用户是否已经存在,另一次保存用户。我认为你是反对票。请考虑一下您的解决方案,然后将我的分数反馈给我。
  • 你不明白,db访问不是问题,它可能需要访问多次,直到验证过程。 ORM 为您承担所有数据库访问。
  • @StevenBenitez 很抱歉打扰您,请您解释一下。谢谢
猜你喜欢
  • 2014-12-17
  • 2011-12-27
  • 2013-11-03
  • 2013-11-30
  • 1970-01-01
  • 2019-04-06
  • 2014-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多