在Dispatch Action中的请求中发送的JavaBean中填充Struts 2中的表单字段

时间:2013-07-02 15:32:02

标签: struts-1

我正在开发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通知填充表单,但仅当validateaction-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.htmlwhy does struts reset my form after failed validation?(以及附加到该帖子struts validation occurring when page loads instead of on submit的链接),但这些解决方案都没有解决我正在扩展DispatchAction类的实现。

0 个答案:

没有答案