我有一个注册程序。当我在数据库中插入一条记录时,我将实例化一个类并调用该方法insert()
。当我插入相同的记录时,当然会出现重复数据错误和大量错误消息。我想捕捉它try
and 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>
有addActionError
and addFieldError
方法在ActionSupport
。您可以捕获其中的任何错误validate
方法。如果提交了错误的数据,则调用这些方法。一旦应用,请求将发送至input
结果。在 JSP 中您可以使用<s:actionerror
and <s:fielderror
显示您通过上述方法添加的错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)