我正在开发 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 填写的表单,但仅当in中的validate
属性设置为时才会执行此方法。如果设置为我的 JavaBeanaction-mappings
struts-config.xml
false
validate
true
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
类中定义的动作- 在验证失败的情况下 - 使用先前请求中的值填写用户注册表单(因此用户不必再次重新输入值)。
有人可以帮助我吗?
我已经浏览了http://struts.apache.org/release/2.3.x/docs/how-do-we-repopulate-controls-when-validation-fails.html,为什么 struts 会在验证失败后重置我的表单?(并且附加到该线程的链接在页面加载而不是在提交时发生验证)但是这些解决方案都没有解决我正在扩展 DispatchAction
类的实现。