如何在Action类之外的Struts2中将错误消息发布到HTML

时间:2012-11-25 20:06:38

标签: java spring validation jsp struts2

我有一个注册程序。当我在数据库中插入记录时,我将实例化一个类并调用方法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>

2 个答案:

答案 0 :(得分:1)

addActionError处有addFieldErrorActionSupport种方法。您可以在validate方法中捕获任何错误。如果提交了错误的数据,请调用这些方法。应用后,请求将发送到input结果。在JSP中你可以使用 <s:actionerror<s:fielderror显示您通过上述方法添加的错误。

答案 1 :(得分:0)

试试这个:

1.定义你自己的异常类

public class MyException extends Exception {}

2.如果您的insert方法

中存在例外,请抛出此例外
if(insertError){
    throw new MyExecption("your message");
}

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

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

4.您可以使用<s:actionerror/>

在jsp中显示消息