预填充表格';在Struts2中具有bean默认值的文本字段
在Struts 1.x中,我使用表单bean预先填充表单文本字段,默认值如下:预填充表格';在Struts2中具有bean默认值的文本字段,struts2,Struts2,在Struts 1.x中,我使用表单bean预先填充表单文本字段,默认值如下: <html:form action="/actions/signup1"> First name: <html:text property="firstName"/><BR> 但是在Struts 2.x中,当我尝试如下使用Struts标记textfield时,它没有从bean中预填充默认值 <s:form action="signup1"> <s:tex
<html:form action="/actions/signup1">
First name: <html:text property="firstName"/><BR>
但是在Struts 2.x中,当我尝试如下使用Struts标记textfield时,它没有从bean中预填充默认值
<s:form action="signup1">
<s:textfield name="formBean.firstName" label = "First Name" />
请让我知道这是否可以在请求级别而不是会话级别完成。
提前谢谢
struts.xml
<struts>
<constant name="struts.devMode" value="true" />
<package name="basicstruts2" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
<action name="signup">
<result>/SignUp.jsp</result>
</action>
<action name="signup1" class="coreservlets.action.SignupAction1" method="execute">
<result name="success">/SignUp-Confirmation.jsp</result>
<result name="error">/SignUp.jsp</result>
</action>
</package>
</struts>
将默认值放入构造函数:
public class ContactFormBean {
private String firstname;
public ContactFormBean (){
this.firstName = "First name";
}
}
使用
getFormBean
方法返回nullformBean
对象,
然后,永远不会调用构造函数,尝试访问formBean属性firstName
时会出现一个错误(没有显示该错误,因为它被JSP上的Struts2标记包装)
你可以
1) 在execute方法上实例化它:
public String execute() throws Exception {
this.formBean = new ContactFormBean();
}
2) 在getter上惰性地实例化它:
public ContactFormBean getFormBean(){
if (formBean==null)
this.formBean = new ContactFormBean();
return this.formBean;
}
编辑(根据您的评论): 我不知道您是如何构建web应用程序的 但是,如果您在JSP上,则在呈现JSP之前会调用一个操作(及其execute()方法,或者指定的另一个方法) 因此,无论您在提交后调用ActionOne加载内容和ActionTwo,或者在提交后调用ActionOne加载内容和ActionOne的另一个方法,您都可以知道您处于“前JSP”状态还是“后提交”状态 也就是说,如果要公开一个对象,并且希望其值或属性不同于
null
,则必须用上述方法之一对其进行实例化
显然,对象应该包含其属性的getter和setter,并且它们必须已绑定到JSP对象
在您的示例中,您可以:
ContactFormBean:
public class ContactFormBean {
private String firstName = "First name";
public String getFirstName(){
return this.firstName;
}
public String setFirstName(String firstName){
this.firstName = firstName;
}
}
你的行动:
public class SignupAction1 extends ActionSupport {
private ContactFormBean formBean;
public ContactFormBean getFormBean(){
return formBean;
}
public void setFormBean(ContactFormBean fb){
formBean = fb;
}
@Override
public String execute() throws Exception {
/* this method is executed before showing the JSP */
/* first time initialization */
this.formBean = new ContactFormBean();
return SUCCESS;
}
public String loginAfterSubmit() throws Exception {
/* this method is executed after the submit button was pressed */
/* i don't initialize nothing here,
ContactFormBean is coming from the page */
System.out.println("ContactFormBean's firstName value is: " +
this.formBean.getFirstName());
return "goSomewhereElse";
}
}
在JSP中:
<s:form>
<s:textfield name="formBean.firstName" label = "First Name" />
<s:submit action="signup1" method="loginAfterSubmit" value="Press here to submit" />
</s:form>
通过将execute()和loginAfterSubmit()方法拆分为两个不同操作的execute()方法,您可以通过两个操作来实现这一点
然后您必须在两个操作中都使用formBean,至少第一个是getter,第二个是setter。将您的
注册
操作声明更改为
<action name="signup" class="coreservlets.action.SignupAction1">
<result>/SignUp.jsp</result>
</action>
在
SignupAction1
操作类中创建方法signup1
,并将逻辑从execute
移动到它。在execute
中,然后创建您的ContactFormBean的新实例
您还可以在execute方法中设置bean中的Name值,它将填充到JSP页面中。例如:
public String execute() throws Exception {
this.formBean = new ContactFormBean();
//Set your Name value here
formBean.setName("John Doe");
return SUCCESS;
}
填充JSP后,您将在名称文本框中找到该值。
希望这会有所帮助
Tapan好吧,在execute方法上实例化formBean会将表单输入的值过度隐藏到bean字段中!!也就是说,每次单击submit按钮,控件转到Action类的execute方法时,字符串firstName=formBean.getFirstName();它选择默认值,而不是表单输入的值。所以我不应该在execute方法中包含业务逻辑?而且我认为只有在调用Action类之后才会实例化formBean。所以,当第一次呈现jsp表单时,它无法获取值。我的意思是,只有在按下submit按钮并将控件转到Action类之后,bean才会被实例化,并且在返回success或failure并在struts.xml中相应地映射到此jsp之后,才会从bean填充此文本字段。因此,我的问题是在初始呈现本身时填充jsp表单。希望你是清楚的。按照你说的去做,但仍然没有成功。让我把包括struts.xml在内的全部代码粘贴到上面的注释下面,请检查我是否按照你的方式做了。谢谢!成功了。我以前没有为struts.xml中的注册jsp配置Action类Configurated,这就是问题所在。@AleksandrM:我很确定
name
对于textfield来说已经足够了,value
是不需要的,除非您想从getter中显示与直接值不同的内容:)@AndreaLigios:是的,您是对的。将从我的答案中删除此内容。
public class ContactFormBean {
private String firstName = "First name";
public String getFirstName(){
return this.firstName;
}
public String setFirstName(String firstName){
this.firstName = firstName;
}
}
public class SignupAction1 extends ActionSupport {
private ContactFormBean formBean;
public ContactFormBean getFormBean(){
return formBean;
}
public void setFormBean(ContactFormBean fb){
formBean = fb;
}
@Override
public String execute() throws Exception {
/* this method is executed before showing the JSP */
/* first time initialization */
this.formBean = new ContactFormBean();
return SUCCESS;
}
public String loginAfterSubmit() throws Exception {
/* this method is executed after the submit button was pressed */
/* i don't initialize nothing here,
ContactFormBean is coming from the page */
System.out.println("ContactFormBean's firstName value is: " +
this.formBean.getFirstName());
return "goSomewhereElse";
}
}
<s:form>
<s:textfield name="formBean.firstName" label = "First Name" />
<s:submit action="signup1" method="loginAfterSubmit" value="Press here to submit" />
</s:form>
<action name="signup" class="coreservlets.action.SignupAction1">
<result>/SignUp.jsp</result>
</action>
<action name="signup1" class="coreservlets.action.SignupAction1" method="signup1">
public String execute() throws Exception {
this.formBean = new ContactFormBean();
//Set your Name value here
formBean.setName("John Doe");
return SUCCESS;
}