I have a register program. When I insert a record on the database, I'll instantiate a class and invoke the method insert()
. When I insert a the same record, of course there is a duplicate data error and tons of error messages. I want to capture them with try-catch
. I can do that. However, I have no idea how to display the error messages to the JSP.
What I have understood, in an action class, there's validate()
method, and the validation.xml
is running first. The insert duplicate error happened after those methods are invoked.
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;
}
Here is my insert user profile method of
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();
}
This is my 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>
try this:
1.define your own exception class
public class MyException extends Exception {}
2.throw this exception, if there is an exception in your insert
method
if(insertError){
throw new MyExecption("your message");
}
3.in the action class, invoke insert method with try-catch block
try{
insert();
} catch(MyException me){
addActionError(me.getMessage());
return "anything_you_want";
}
4.you can show the message in the jsp using <s:actionerror/>