3

私は登録プログラムを持っています。データベースにレコードを挿入すると、クラスがインスタンス化され、メソッドが呼び出されますinsert()。同じレコードを挿入すると、もちろん重複データ エラーと大量のエラー メッセージが表示されます。tryとでキャプチャしたいcatch。私はそれを行うことができます。ただし、メッセージを 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>
4

2 に答える 2

1

にはaddActionErroraddFieldErrorメソッドがありActionSupportます。validateメソッド内でエラーをキャッチできます。間違ったデータが送信された場合は、これらのメソッドを呼び出します。適用されると、リクエストは結果にディスパッチされinputます。JSP では、 上記の方法で追加したエラーを表示する ため<s:actionerrorに とを使用できます。<s:fielderror

于 2012-11-25T20:31:09.050 に答える
0

これを試して:

1.独自の例外クラスを定義する

public class MyException extends Exception {}

insert2.メソッドに例外がある場合は、この例外をスローします

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.を使用してjspにメッセージを表示できます<s:actionerror/>

于 2012-11-25T21:47:17.167 に答える