gpt4 book ai didi

java - 在 FormBean 中设置属性之前如何抛出异常或设置 ActionError

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:10 24 4
gpt4 key购买 nike

我有以下 formbean:

public class EmployeeForm extends org.apache.struts.action.ActionForm {

private int empid,age;

public int getEmpid() {
return empid;
}

public void setEmpid(int empid) {
this.empid = empid;
}

public int getAge() {
return age;
}

public void setAge(int age) {
if(age>20)
this.age = age;

}

public String getEmpname() {
return empname;
}

public void setEmpname(String empname) {
this.empname = empname;
}

public String getPosition() {
return position;
}

public void setPosition(String position) {
this.position = position;
}
private String empname,position;

public EmployeeForm() {
super();
// TODO Auto-generated constructor stub
}

/**
* This is the action called from the Struts framework.
*
* @param mapping The ActionMapping used to select this instance.
* @param request The HTTP Request we are processing.
* @return
*/
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getEmpname() == null || getEmpname().length() < 1) {
errors.add("name", new ActionMessage("error.name.required"));
// TODO: add 'error.name.required' key to your resources
}
return errors;
}
}

现在,我想在用户输入小于 20 岁的年龄时显示错误。怎么做? bean 本身是否有可能抛出错误?

以下是指向 ActionObject 的 View :

<html:errors/>
<html:form action="EmployeeAction" >

<table>
<tr><td>Enter employee Id</td>
<td><html:text name="EmployeeForm" property="empid" /></td>
</tr>

<tr><td>Enter employee Name</td>
<td><html:text name="EmployeeForm" property="empname" /></td>
</tr>
<tr><td>Enter employee Age</td>
<td><html:text name="EmployeeForm" property="age" /></td>
</tr>
<tr><td>Enter employee position</td>
<td><html:text name="EmployeeForm" property="position" /></td>
</tr>


<tr><td align="right"><html:submit value="Add" property="method" /></td>
<td> <html:submit property="method" value="Delete" />

<html:submit property="method">Update</html:submit></td>
</tr>


</table>
</html:form>

最佳答案

定义获取消息资源的辅助方法

public static MessageResources getResources(HttpServletRequest request) {
return ((MessageResources) request.getAttribute(Globals.MESSAGES_KEY));
}

然后使用资源

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
try {
MessageResources resources = getResources(request);
ActionErrors errors = super.validate(mapping, request);
if (getEmpname() == null || getEmpname().length() < 1) {
errors.add("name", new ActionMessage("error.name.required"));
throw new Exception(getResources(request).getMessage("error.name.required"));
}
return errors;
} catch (Exception ex) {
request.setAttribute(Globals.EXCEPTION_KEY, ex);
return null;
}
}

检查请求处理器中的异常键

// Check out if exception occurred
Exception exception = (Exception)request.getAttribute(Globals.EXCEPTION_KEY);
if (exception != null){
ActionForward forward = processException(request, response, exception, form, mapping);
processForwardConfig(request, response, forward);
return;
}

关于java - 在 FormBean 中设置属性之前如何抛出异常或设置 ActionError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23289766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com