我正在开发JavaEE中的用户管理模块,Struts2框架中的Glassfish, 我目前正在注册模块。
我遇到的问题是我想避免使用者'沮丧并实现这一点,我想在当前呈现的页面中保留先前请求中填写的值。
我的架构如下:
我有jsp页面包含:
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<jsp:useBean id="user" scope="session" class="my.struts.UserAddFormBean">
...
<html:errors/>
...
<form action="userAdd.do?action=add" method="POST" class="form-horizontal">
...
<input type="text" id="new_username" name="username" placeholder="User name" value="<bean:write name="user" property="username" />">
...
为了对连接到用户的所有操作进行分组,而不是org.apache.struts.Action
类,我使用的是org.apache.struts.DispatchAction
public class UserActionDispatcher extends DispatchAction {
public ActionForward add(ActionMapping mapping, ActionForm form2,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
...
request.setAttribute("user", form); //this informs the bean about filled form, but this method is executed only if validate attribute in action mappings in struts-config.xml is set to false
...
...
return mapping.findForward(SUCCESS);
}
request.setAttribute("user", form);
向bean通知填充表单,但仅当validate
中action-mappings
中的struts-config.xml
属性设置为false
时,才会执行此方法。如果validate
设置为true
我的JavaBean org.apache.struts.action.ActionForm
public class UserAddFormBean extends org.apache.struts.action.ActionForm {
private String username, password;
public String getUsername();
public void setUsername(String username); //and other getters and setters
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
//This is simplified validate
ActionErrors errors = new ActionErrors();
errors.add("useradd", new ActionMessage("error.username.required"));
return errors;
}
}
我的struts-config.xml
包含以下action
定义:
<struts-config>
<form-beans>
<form-bean name="UserAddFormBean" type="my.struts.UserAddFormBean"/>
</form-beans>
<action-mappings>
<action input="/user_add.jsp"
name="UserAddFormBean"
path="/userAdd"
scope="request" //even if this is set to session, if validate is set to true, then my.struts.UserActionDispatcher.edit is not called, therefore form fields are NOT being sent to action-mappings.input="/user_add.jsp" file, because request.save(...) line is NOT executed
type="my.struts.UserActionDispatcher"
validate="true"
parameter="action">
<!--Based on http://struts.apache.org/release/2.3.x/docs/comparing-struts-1-and-2.html any class which implements execute method can be Action controller-->
<forward name="success" path="/user_add.jsp"/>
</action>
</action-mappings>
我想:
DispachAction
class 有人可以帮助我吗?
我已经浏览了http://struts.apache.org/release/2.3.x/docs/how-do-we-repopulate-controls-when-validation-fails.html和why does struts reset my form after failed validation?(以及附加到该帖子struts validation occurring when page loads instead of on submit的链接),但这些解决方案都没有解决我正在扩展DispatchAction
类的实现。