2010-11-01 39 views
0

这里是情况。Struts问题;使用相同的动作来显示并提交

我有一个页面叫做param.jsp,它只有一个表单和一个提交按钮。数据库中有一条记录,当表单呈现时,我想用该记录填充表单。提交表单时,我想更新该单个记录并返回到同一页面。在struts中执行此操作的最佳方法是什么?

到目前为止,我已经想出了这个;这里是动作:

class MyAction extends DispatchAction{ 
    public ActionForward savePlatinumJLParam(......){ 
     //<insert the form to the database> 
     return mapping.findForward("<return to the same page>"); 
    } 
    public ActionForward initPlatinumJLParam(......){ 
     //<load the form from the database> 
     //form.setXX(...); 
     return mapping.findForward("<return to the same page>"); 
    } 
} 

节能工作得很好,但我有与填充表格的麻烦。任何帮助表示赞赏。

回答

0

当您渲染JSP时,您需要访问bean中的变量来设置表单元素的值。

可能会是这个样子,

<input type="text" name="username" value="<%= someBean.getSomeField() >"/> 

在这里阅读更多,http://struts.apache.org/1.x/userGuide/building_view.html

我知道这不是你的问题,但使用相同的动作,以保存和展示我不会reccomend。我将有一个操作来保存数据,并显示一个不同的操作。然后,当您将数据提交到保存操作时,重定向到显示操作。 This a question about redirecting.

0

如果你已经宣布struts-config.xml像这样的动作(假设NAME = “submitForm”已声明):

<form-beans> 
    <form-bean name="submitForm" type="hansen.playground.SubmitForm"/> 
</form-beans> 

<action path="/submit" 
       type="hansen.playground.SubmitAction" 
       name="submitForm" 
       input="/submit.jsp" 
       scope="request"> 
</action> 

和你的形式是这样的:

package hansen.playground; 
public class SubmitForm extends ActionForm { 
    private String name; 
    private String contactEmail; 

    //Getters and setters are here.... 

} 

然后你可以在你的Struts DispatchAction(在我的情况下,SubmitAction)做到这一点:

package hansen.playground; 
public class SubmitAction extends DispatchAction{ 
    public ActionForward request(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){ 
     //<insert the form to the database> 
     ((SubmitForm)form).setName("The Elite Gentleman"); 
     ((SubmitForm)form).setContactEmail("[email protected]"); 

     return mapping.findForward("<return to the same page>"); 
    } 
} 

因为您的ActionForm映射到您的Struts Action,所以当调用request方法时,Struts会将SubmitForm发送到ActionForm form。将<action>标记上的name更改为另一个ActionForm,Struts将根据请求发送该表单。

希望这有助于...


编辑在输出时,你就必须从submitForm这样显示你的结果:

<html:text name="submitForm" property="name" /> 

(见的name属性匹配 Struts表单名称)。

+0

感谢您的回答,但这正是我所做的,并没有奏效。我一定会错过一些东西,我会尝试你以后提供的这个例子并发布结果。 – 2010-11-01 17:57:17

+0

你得到了什么异常? – 2010-11-01 18:02:44

+0

我没有收到任何异常,表单字段呈现为空。 – 2010-11-02 06:05:26