【问题标题】:Struts 1 dynamically set the input attribute of the actionStruts 1 动态设置action的input属性
【发布时间】:2014-02-04 19:27:18
【问题描述】:

我正在使用 Struts 1 开发一个 Java 库项目(我知道它很旧,但这是一个学校项目)。

我使用 op 参数创建了一个 DispatchAction 来处理不同方法(添加、编辑、删除)的 Book 请求。

add 方法可以正常工作,因为它有类似 /book.do?op=add 的 url,但 edit 有一个概念性问题,它使用类似 book.do?op=edit&id=1 的动态 url:

如果验证发现一些错误,动作怎么知道在哪里 重定向?输入属性是静态的,但是发送请求的页面有一个动态的url。

我的 struts-config.xml 中有这个:

     <!-- Base Book Action -->
    <action path="/book" type="com.github.jcpp.jathenaeum.actions.BookAction" parameter="op">
        <forward name="add" path="page.addBook"/>
        <forward name="edit" path="page.editBook"/>
        <forward name="delete" path="page.deleteBook"/>
        <forward name="viewAll" path="page.viewAllBooks"/>
        <forward name="noAuthors" path="page.noAuthors" />
    </action>

    <!-- Action Book -->
    <action
        path="/doBook"
        type="com.github.jcpp.jathenaeum.actions.BookActionDo"
        name="bookForm"
        scope="request"
        validate="true"
        parameter="op">
        <forward name="add" path="page.confirmBookInsert"/>
        <forward name="edit" path="page.confirmBookInsert"/>
        <forward name="success" path="page.confirmBookInsert"/>
        <forward name="failed" path="page.failedBookInsert"/>
    </action>

我想知道的是:这是做这种事情的最好方法吗?还是有更简单的方法?

可以动态设置输入属性的值吗?

如果您愿意,可以在 GitHub 上查看整个项目代码:https://github.com/JCPP/JAthenaeum

更新 1

我的问题是在验证后重定向到这样的动态网址:book.do?op=edit&id=1。您可以更改的书可以有不同的 id (29, 50, 49)。在这种情况下,输入属性是窄的。

谢谢

【问题讨论】:

    标签: java struts struts-1


    【解决方案1】:

    我认为您不能动态更改输入属性的值。

    当您的表单提交但验证失败时,它会被重定向到"input" 属性中定义的页面。

    我认为不需要动态更改“输入”属性。当验证失败时,您可以将其重定向回用户输入数据的同一页面,错误消息显示无效字段。

    当您输入无效数据时,您可以在网站的任何“注册/登录”页面中看到这种行为。

    编辑

    这是一种解决方法。在映射文件中设置validate="false"。并从您的操作类中调用验证方法。 您还可以更改 validate 方法的返回类型以满足您的需要(例如字符串)。 因此,在您的操作类中,根据 validate 方法返回的值,您可以创建不同的 url 并将其转发到那里:

    public class MyAction extends DispatchAction{
    
    public ActionForward add(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response,
                ActionMessages messages) throws Exception
    
           MyForm myForm = (MyForm) form;
           String url="";
           String result = form.validate();
    
           if(result.equals("something")){
                  url="book.do?op=add&id=" + book.getId();
            }else if(result.equals("somethingElse"){
                  url="book.do?op=edit&id=" + book.getId();
           }
    
           RequestDispatcher rd = request.getSession().getServletContext().getRequestDispatcher(url);
           rd.forward(request, response);
    }
    

    【讨论】:

    • 我想要得到的有点不同。我的表单在我想在操作之间保存的 url 中有一个 get 变量。看看我的更新。
    • 这是个好主意。最后我选择了类似的选项。我将创建另一个答案。
    【解决方案2】:

    有一些东西需要设置。第一件事。

    Struts 配置

    在 BookAction 中,应用程序将显示表单,而在 BookActionDo 中,它将更新数据库中的图书。

    <!-- Book -->
    <action path="/book" type="com.github.jcpp.jathenaeum.actions.BookAction" parameter="op">
      <forward name="edit" path="page.book.edit"/>
    </action>
    
    <!-- Book Operations -->
    <action
      path="/doBook"
      type="com.github.jcpp.jathenaeum.actions.BookActionDo"
      name="bookForm"
      scope="request"
      validate="false"
      parameter="op">
    
      <!-- Edit-->
      <forward name="editErrors" path="/book.do?op=edit" redirect="true"/>
      <forward name="editSuccess" path="page.book.edit.confirm" />
      <forward name="editFailed" path="page.book.edit.failed" />
    

    这里告诉struts不要用validate="false"验证表单。

    动作

    public class BookActionDo extends DispatchAction {
    
     public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
        String actionTarget = null;
    
        HttpSession session = request.getSession();
    
        Book book = new Book();
        BookForm uf = (BookForm) form;
    
        //Check the id
        if(!uf.validateId(mapping, request).isEmpty()){
            actionTarget = "invalidId";
    
            ActionRedirect redirect = new ActionRedirect(mapping.findForward(actionTarget));
            return redirect;
        }
    
        ActionErrors actionErrors = uf.validate(mapping, request);
        int id = Integer.parseInt(request.getParameter("id"));
    
        //If there are some errors, redirect to the form page
        if(!actionErrors.isEmpty()){
            actionTarget = "editErrors";
    
            session.setAttribute("errors", actionErrors);
            session.setAttribute("form", uf);
    
            ActionRedirect redirect = new ActionRedirect(mapping.findForward(actionTarget));
            redirect.addParameter("id", Integer.toString(id));
            return redirect;
        }
    
        if(form != null){
            book = new Book(uf);
            book.setId(id);
    
            try{
                int bookId = (int) BookDAO.update(book);
    
            }catch(Exception e){
                actionTarget = "editFailed";
            }
        }
    
        return mapping.findForward(actionTarget);
     }
    }
    

    这里它检查表单的验证是否返回一些ActionErrors。如果是,它会创建一个带有id 参数的ActionRedirect,否则它会尝试更新图书。

    所以当它创建 url 时,ActionRedirect 将重定向到:/book.do?op=edit&amp;id=&lt;book_id&gt;。

    【讨论】:

      猜你喜欢
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      • 2018-12-13
      相关资源
      最近更新 更多