在Struts 1.x中,我使用表单bean预填充表单文本字段,默认值如下,
<html:form action="/actions/signup1">
First name: <html:text property="firstName"/><BR>
在表单bean中,我有以下默认值...
public class ContactFormBean {
private String firstName = "First name";
但是在Struts 2.x中,当我尝试使用struts-tags文本字段时,它没有预先填充bean的默认值,
<s:form action="signup1">
<s:textfield name="formBean.firstName" label = "First Name" />
我在我的Action类中使用适当的getter和setter方法声明了formBean ...
public class SignupAction1 extends ActionSupport {
private ContactFormBean formBean;
@Override
public String execute() throws Exception {
....
}
public ContactFormBean getFormBean(){
return formBean;
}
public void setFormBean(ContactFormBean fb){
formBean = fb;
}
}
如果可以在请求级别而非会话级别完成此操作,请告知我们。 提前谢谢。
&LT; - 编辑 - &GT;
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>
SignUp.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sign UP</title>
</head>
<body>
<H1 ALIGN="CENTER">Sign Up</H1>
<s:form>
<s:textfield name="formBean.firstName" label = "First Name" />
<s:textfield name="formBean.lastName" label = "Last Name" />
<s:textfield name="formBean.email" label = "Email Address" />
<s:textfield name="formBean.faxNumber" label = "Fax Number" />
<s:submit action="signup1" method="loginAfterSubmit" value="Click here to Submit"/>
</s:form>
</body>
</html>
ContactFormBean.java
public class ContactFormBean {
private String firstName = "First name";
private String lastName = "Last name";
private String email = "user@host";
private String faxNumber = "xxx-yyy-zzzz";
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return(lastName);
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return(email);
}
public void setEmail(String email) {
this.email = email;
}
public String getFaxNumber() {
return(faxNumber);
}
public void setFaxNumber(String faxNumber) {
this.faxNumber = faxNumber;
}
public boolean isMissing(String value) {
if ((value == null) || (value.trim().equals(""))) {
return(true);
} else {
for(int i=0; i<defaultValues.length; i++) {
if (value.equals(defaultValues[i])) {
return(true);
}
}
return(false);
}
}
}
SignupAction1.java
public class SignupAction1 extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private ContactFormBean formBean;
@Override
public String execute() throws Exception {
this.formBean = new ContactFormBean();
return SUCCESS;
}
public String loginAfterSubmit() {
String firstName = formBean.getFirstName();
String lastName = formBean.getLastName();
String email = formBean.getEmail();
String faxNumber = formBean.getFaxNumber();
if (formBean.isMissing(firstName)) {
return ERROR;
} else if (formBean.isMissing(lastName)) {
return ERROR;
} else if ((formBean.isMissing(email)) ||
(email.indexOf("@") == -1)) {
return ERROR;
} else if (formBean.isMissing(faxNumber)) {
return ERROR;
} else {
return SUCCESS;
}
}
public ContactFormBean getFormBean(){
return this.formBean;
}
public void setFormBean(ContactFormBean fb){
formBean = fb;
}
}
答案 0 :(得分:1)
将您的signup
操作声明更改为
<action name="signup" class="coreservlets.action.SignupAction1">
<result>/SignUp.jsp</result>
</action>
和signup1
到
<action name="signup1" class="coreservlets.action.SignupAction1" method="signup1">
在signup1
动作类中创建方法SignupAction1
,并将逻辑从execute
移至它。在execute
中,然后创建ContactFormBean
的新实例。
答案 1 :(得分:0)
将默认值设置为构造函数:
public class ContactFormBean {
private String firstname;
public ContactFormBean (){
this.firstName = "First name";
}
}
答案 2 :(得分:0)
您使用formBean
方法返回null getFormBean
对象,
那么构造函数永远不会被调用,并且尝试访问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之前调用Action(及其execute()方法或其他方法(如果已指定))。
因此,无论你是否在提交后调用ActionOne和ActionTwo,或者在提交后调用ActionOne和ActionOne的另一个方法,你可以知道你是处于“pre-JSP”状态还是“post” - 提交“州......
也就是说,如果您要公开一个Object,并且您希望它的值或其属性与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()方法分成两个不同Actions的两个execute()方法来实现两个Action。
然后你必须将你的formBean放在两个动作中,至少第一个是getter,第二个是setter。
答案 3 :(得分:0)
您还可以在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后,您将在“名称”文本框中找到此值。 希望这会有所帮助。
塔潘