You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by John Norvell <92...@cox.net> on 2008/09/17 02:13:40 UTC
session based pojo as form variables
Hi,
I'm new to struts2 and old with struts 1.x. My application is 99%
wizards using session based form beans.
I'm trying to write my own simple struts2 test app with a single form,
single action, and a pojo valueobj saved in session.
The action has 2 functions: one to create the pojo and store it in
session then redirect to the JSP page that draws the form. The form is
displaying the default values perfectly. The second function is the
form submission.
When I edit the values and submit the form, the submit() function is
being called, but I cannot find where the updated values are being
stored. They are not copied into my pojo automatically. When I
retrieve it from session it contains the original default values.
Is there simple answer to this? I can include some code snippets if
necessary.
Thanks.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: session based pojo as form variables
Posted by Gabriel Belingueres <be...@gmail.com>.
1) Again, replace the names of your form fields:
from: <s:textfield
name="#session.formBean.firstName"value="%{#session.formBean.firstName}"
label="First Name" size="16" />
to: <s:textfield name="formBean.firstName" label="First Name" size="16" />
2) SessionAware interface: it is pointless to implement it if you are
not going to save the session Map in an instance variable for future
reference, and if getting the session through Map session =
(Map)ActionContext.getContext().get("session"); works, it is not the
preferred way of getting it [1]
3) javascript code: Note that if the user press Enter key on a
textfield, the form will be submitted without assigning your target
hidden field.
[1] http://struts.apache.org/2.1.2/docs/how-do-we-get-access-to-the-session.html
2008/9/19 Dave Newton <ne...@yahoo.com>:
> As was mentioned to you previously, at the very least you shouldn't use "#session..." as the name; you should just use "session" if you've implemented a getSession() method. That may be enough; can't test at the moment.
>
> Dave
>
> --- On Fri, 9/19/08, 928572663 <92...@cox.net> wrote:
>> I am using Zero Configuration (annotations), so no
>> struts.xml necessary.
>>
>> ----------------
>> formDetails.jsp
>> ----------------
>>
>> <!DOCTYPE html PUBLIC "~//W3C//DTD XHTML 1.0
>> Strict//EN"
>> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
>>
>> <%@ page contentType="text/html;charset=UTF-8"
>> language="java"%>
>> <%@ taglib prefix="s"
>> uri="/struts-tags"%>
>>
>> <html>
>> <head>
>> <title>IPSM struts2 Demo Form</title>
>> <link href="<s:url value
>> ='/css/main.css'/>"
>> rel="stylesheet"
>> type="text/css" />
>> </head>
>> <body>
>> <h2>
>> <s:property value="message" />
>> </h2>
>>
>> <s:actionerror />
>> <s:form action="sample!submit"
>> method="post">
>>
>> <s:checkbox
>> name="#session.formBean.requery"
>> label="Requery?" />
>>
>> <s:textfield
>> name="#session.formBean.firstName"
>> value="%{#session.formBean.firstName}"
>> label="First Name"
>> size="16" />
>>
>> <s:textfield
>> name="#session.formBean.maxValue"
>> value="%{#session.formBean.maxValue}"
>> label="Max Value"
>> size="10" /> (<s:property
>> value="%{#session.formBean.maxValueRangeMin}"
>> />-<s:property
>> value="%{#session.formBean.maxValueRangeMax}"
>> />)
>>
>> <s:textfield
>> name="#session.formBean.lastName"
>> value="%{#session.formBean.lastName}"
>> label="Last Night"
>> size="10" required="true" />
>>
>> <s:label
>> key="#session.formBean.maxValueRangeMax"
>> label="Max Routes (Max)" />
>>
>> <s:textarea label="Comment"
>> name="#session.formBean.comment"
>> cols="15" rows="8" />
>>
>> <s:select
>> name="#session.formBean.selection"
>> value="%{#session.formBean.selection}"
>> label="Selection"
>> list="#session.formBean.picklist"
>> listKey="code"
>> listValue="label" />
>>
>> <s:hidden name="target"
>> value="setme" />
>>
>> <s:url var="back_button_url"
>> value="/images/back_button.gif" />
>> <s:submit type="image"
>> src="%{#back_button_url}" label=""
>> cssClass="formButton"
>> onclick="javascript:this.form.target.value='backButton';
>>
>> this.form.submit();" />
>>
>> <s:url var="next_button_url"
>> value="/images/next_button.gif" />
>> <s:submit type="image"
>> src="%{#next_button_url}" label=""
>> cssClass="formButton"
>> onclick="javascript:this.form.target.value='nextButton';
>>
>> this.form.submit();" />
>>
>> </s:form>
>>
>> </body>
>> </html>
>>
>> ----------------
>> MyFormBean.jsp
>> ----------------
>>
>> package sample;
>>
>> import java.util.ArrayList;
>> import java.util.List;
>>
>> public class MyFormBean
>> {
>> private boolean requery = true;
>> private String firstName = "";
>> private String selection = "";
>> private String lastName = "";
>> private String comment = "";
>> private int maxValue = 0;
>>
>> //~~~ items for GUI display purposes etc.
>> private final int maxValueRangeMin = 1;
>> private final int maxValueRangeMax = 263732;
>> private List<PicklistItem> picklist = null;
>> //~~~
>>
>> public MyFormBean()
>> {
>> picklist = new ArrayList<PicklistItem>();
>> picklist.add(new
>> PicklistItem("1","Item 1"));
>> picklist.add(new
>> PicklistItem("2","Item 2"));
>> picklist.add(new
>> PicklistItem("3","Item 3"));
>> picklist.add(new
>> PicklistItem("4","Item 4"));
>> picklist.add(new
>> PicklistItem("5","Item 5"));
>> }
>>
>> public boolean isRequery()
>> {
>> return requery;
>> }
>>
>> public void setRequery(boolean requery)
>> {
>> this.requery = requery;
>> }
>>
>> public String getFirstName()
>> {
>> return firstName;
>> }
>>
>> public void setFirstName(String firstName)
>> {
>> this.firstName = firstName;
>> }
>>
>> public String getSelection()
>> {
>> return selection;
>> }
>>
>> public void setSelection(String selection)
>> {
>> this.selection = selection;
>> }
>>
>> public String getLastName()
>> {
>> return lastName;
>> }
>>
>> public void setLastName(String lastName)
>> {
>> this.lastName = lastName;
>> }
>>
>> public String getComment()
>> {
>> return comment;
>> }
>>
>> public void setComment(String comment)
>> {
>> this.comment = comment;
>> }
>>
>> public int getMaxValue()
>> {
>> return maxValue;
>> }
>>
>> public void setMaxValue(int maxValue)
>> {
>> this.maxValue = maxValue;
>> }
>>
>> public List<PicklistItem> getPicklist()
>> {
>> return picklist;
>> }
>>
>> public void setPicklist(List<PicklistItem>
>> picklist)
>> {
>> this.picklist = picklist;
>> }
>>
>> public int getMaxValueRangeMin()
>> {
>> return maxValueRangeMin;
>> }
>>
>> public int getMaxValueRangeMax()
>> {
>> return maxValueRangeMax;
>> }
>> }
>>
>> ----------------
>> PicklistItem.jsp
>> ----------------
>>
>> package sample;
>>
>>
>> public class PicklistItem
>> {
>> private String code = "";
>> private String label = "";
>>
>> public PicklistItem(String aCode, String aLabel)
>> {
>> code = aCode;
>> label = aLabel;
>> }
>>
>> public String getCode()
>> {
>> return code;
>> }
>>
>> public void setCode(String code)
>> {
>> this.code = code;
>> }
>>
>> public String getLabel()
>> {
>> return label;
>> }
>>
>> public void setLabel(String label)
>> {
>> this.label = label;
>> }
>> }
>>
>> ----------------
>> SampleAction.jsp
>> ----------------
>>
>> package sample;
>>
>>
>> import java.util.Map;
>>
>> import org.apache.struts2.config.Result;
>> import org.apache.struts2.config.Results;
>> import org.apache.struts2.interceptor.SessionAware;
>> import org.apache.struts2.views.tiles.TilesResult;
>>
>>
>> import com.opensymphony.xwork2.ActionContext;
>> import com.opensymphony.xwork2.ActionSupport;
>>
>> @Results(
>> {
>> @Result(name = "displayFormPage", value =
>> "/formDetails.jsp"),
>> @Result(name = "homeError", value =
>> "/homeError.page", type =
>> TilesResult.class)
>> } )
>> public class SampleAction extends ActionSupport implements
>> SessionAware
>> {
>> private String message = "";
>> private String target = "";
>> private MyFormBean formBean = new MyFormBean();
>>
>> public void setSession(Map<String, Object>
>> session)
>> {
>> Object obj = session.get("formBean");
>> if (obj == null)
>> {
>> session.put("formBean", formBean);
>> }
>> }
>>
>> public void setFormBean(MyFormBean formBean)
>> {
>> Map session =
>> (Map)ActionContext.getContext().get("session");
>> session.put("formBean", formBean);
>> }
>>
>> public MyFormBean getFormBean()
>> {
>> Map session =
>> (Map)ActionContext.getContext().get("session");
>> return
>> (MyFormBean)session.get("formBean");
>> }
>>
>> public String displayForm() throws Exception
>> {
>> formBean = new MyFormBean();
>> formBean.setComment("struts2 is cool");
>> formBean.setFirstName("Linus");
>> formBean.setLastName("Torvalds");
>> formBean.setMaxValue(50);
>>
>> setFormBean(formBean);
>>
>> // Map session = (Map)
>> ActionContext.getContext().get("session");
>> // session.put("formBean",formBean);
>>
>> return "displayFormPage";
>> }
>>
>> public String submit() throws Exception
>> {
>> if (target != null
>> && target.length() > 0)
>> {
>> setMessage(target
>> + " was pressed.");
>> }
>>
>> // Map session = (Map)
>> ActionContext.getContext().get("session");
>> // formBean =
>> (MyFormBean)session.get("formBean");
>>
>> return "displayFormPage";
>> }
>>
>> public String getMessage()
>> {
>> return message;
>> }
>>
>> public void setMessage(String message)
>> {
>> this.message = message;
>> }
>>
>> public String getTarget()
>> {
>> return target;
>> }
>>
>> public void setTarget(String target)
>> {
>> this.target = target;
>> }
>> }
>>
>>
>>
>>
>> Gabriel Belingueres wrote:
>> > Could you post your struts.xml, action and (one of
>> the) JSP?
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail:
>> user-help@struts.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: session based pojo as form variables
Posted by Dave Newton <ne...@yahoo.com>.
As was mentioned to you previously, at the very least you shouldn't use "#session..." as the name; you should just use "session" if you've implemented a getSession() method. That may be enough; can't test at the moment.
Dave
--- On Fri, 9/19/08, 928572663 <92...@cox.net> wrote:
> I am using Zero Configuration (annotations), so no
> struts.xml necessary.
>
> ----------------
> formDetails.jsp
> ----------------
>
> <!DOCTYPE html PUBLIC "~//W3C//DTD XHTML 1.0
> Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
>
> <%@ page contentType="text/html;charset=UTF-8"
> language="java"%>
> <%@ taglib prefix="s"
> uri="/struts-tags"%>
>
> <html>
> <head>
> <title>IPSM struts2 Demo Form</title>
> <link href="<s:url value
> ='/css/main.css'/>"
> rel="stylesheet"
> type="text/css" />
> </head>
> <body>
> <h2>
> <s:property value="message" />
> </h2>
>
> <s:actionerror />
> <s:form action="sample!submit"
> method="post">
>
> <s:checkbox
> name="#session.formBean.requery"
> label="Requery?" />
>
> <s:textfield
> name="#session.formBean.firstName"
> value="%{#session.formBean.firstName}"
> label="First Name"
> size="16" />
>
> <s:textfield
> name="#session.formBean.maxValue"
> value="%{#session.formBean.maxValue}"
> label="Max Value"
> size="10" /> (<s:property
> value="%{#session.formBean.maxValueRangeMin}"
> />-<s:property
> value="%{#session.formBean.maxValueRangeMax}"
> />)
>
> <s:textfield
> name="#session.formBean.lastName"
> value="%{#session.formBean.lastName}"
> label="Last Night"
> size="10" required="true" />
>
> <s:label
> key="#session.formBean.maxValueRangeMax"
> label="Max Routes (Max)" />
>
> <s:textarea label="Comment"
> name="#session.formBean.comment"
> cols="15" rows="8" />
>
> <s:select
> name="#session.formBean.selection"
> value="%{#session.formBean.selection}"
> label="Selection"
> list="#session.formBean.picklist"
> listKey="code"
> listValue="label" />
>
> <s:hidden name="target"
> value="setme" />
>
> <s:url var="back_button_url"
> value="/images/back_button.gif" />
> <s:submit type="image"
> src="%{#back_button_url}" label=""
> cssClass="formButton"
> onclick="javascript:this.form.target.value='backButton';
>
> this.form.submit();" />
>
> <s:url var="next_button_url"
> value="/images/next_button.gif" />
> <s:submit type="image"
> src="%{#next_button_url}" label=""
> cssClass="formButton"
> onclick="javascript:this.form.target.value='nextButton';
>
> this.form.submit();" />
>
> </s:form>
>
> </body>
> </html>
>
> ----------------
> MyFormBean.jsp
> ----------------
>
> package sample;
>
> import java.util.ArrayList;
> import java.util.List;
>
> public class MyFormBean
> {
> private boolean requery = true;
> private String firstName = "";
> private String selection = "";
> private String lastName = "";
> private String comment = "";
> private int maxValue = 0;
>
> //~~~ items for GUI display purposes etc.
> private final int maxValueRangeMin = 1;
> private final int maxValueRangeMax = 263732;
> private List<PicklistItem> picklist = null;
> //~~~
>
> public MyFormBean()
> {
> picklist = new ArrayList<PicklistItem>();
> picklist.add(new
> PicklistItem("1","Item 1"));
> picklist.add(new
> PicklistItem("2","Item 2"));
> picklist.add(new
> PicklistItem("3","Item 3"));
> picklist.add(new
> PicklistItem("4","Item 4"));
> picklist.add(new
> PicklistItem("5","Item 5"));
> }
>
> public boolean isRequery()
> {
> return requery;
> }
>
> public void setRequery(boolean requery)
> {
> this.requery = requery;
> }
>
> public String getFirstName()
> {
> return firstName;
> }
>
> public void setFirstName(String firstName)
> {
> this.firstName = firstName;
> }
>
> public String getSelection()
> {
> return selection;
> }
>
> public void setSelection(String selection)
> {
> this.selection = selection;
> }
>
> public String getLastName()
> {
> return lastName;
> }
>
> public void setLastName(String lastName)
> {
> this.lastName = lastName;
> }
>
> public String getComment()
> {
> return comment;
> }
>
> public void setComment(String comment)
> {
> this.comment = comment;
> }
>
> public int getMaxValue()
> {
> return maxValue;
> }
>
> public void setMaxValue(int maxValue)
> {
> this.maxValue = maxValue;
> }
>
> public List<PicklistItem> getPicklist()
> {
> return picklist;
> }
>
> public void setPicklist(List<PicklistItem>
> picklist)
> {
> this.picklist = picklist;
> }
>
> public int getMaxValueRangeMin()
> {
> return maxValueRangeMin;
> }
>
> public int getMaxValueRangeMax()
> {
> return maxValueRangeMax;
> }
> }
>
> ----------------
> PicklistItem.jsp
> ----------------
>
> package sample;
>
>
> public class PicklistItem
> {
> private String code = "";
> private String label = "";
>
> public PicklistItem(String aCode, String aLabel)
> {
> code = aCode;
> label = aLabel;
> }
>
> public String getCode()
> {
> return code;
> }
>
> public void setCode(String code)
> {
> this.code = code;
> }
>
> public String getLabel()
> {
> return label;
> }
>
> public void setLabel(String label)
> {
> this.label = label;
> }
> }
>
> ----------------
> SampleAction.jsp
> ----------------
>
> package sample;
>
>
> import java.util.Map;
>
> import org.apache.struts2.config.Result;
> import org.apache.struts2.config.Results;
> import org.apache.struts2.interceptor.SessionAware;
> import org.apache.struts2.views.tiles.TilesResult;
>
>
> import com.opensymphony.xwork2.ActionContext;
> import com.opensymphony.xwork2.ActionSupport;
>
> @Results(
> {
> @Result(name = "displayFormPage", value =
> "/formDetails.jsp"),
> @Result(name = "homeError", value =
> "/homeError.page", type =
> TilesResult.class)
> } )
> public class SampleAction extends ActionSupport implements
> SessionAware
> {
> private String message = "";
> private String target = "";
> private MyFormBean formBean = new MyFormBean();
>
> public void setSession(Map<String, Object>
> session)
> {
> Object obj = session.get("formBean");
> if (obj == null)
> {
> session.put("formBean", formBean);
> }
> }
>
> public void setFormBean(MyFormBean formBean)
> {
> Map session =
> (Map)ActionContext.getContext().get("session");
> session.put("formBean", formBean);
> }
>
> public MyFormBean getFormBean()
> {
> Map session =
> (Map)ActionContext.getContext().get("session");
> return
> (MyFormBean)session.get("formBean");
> }
>
> public String displayForm() throws Exception
> {
> formBean = new MyFormBean();
> formBean.setComment("struts2 is cool");
> formBean.setFirstName("Linus");
> formBean.setLastName("Torvalds");
> formBean.setMaxValue(50);
>
> setFormBean(formBean);
>
> // Map session = (Map)
> ActionContext.getContext().get("session");
> // session.put("formBean",formBean);
>
> return "displayFormPage";
> }
>
> public String submit() throws Exception
> {
> if (target != null
> && target.length() > 0)
> {
> setMessage(target
> + " was pressed.");
> }
>
> // Map session = (Map)
> ActionContext.getContext().get("session");
> // formBean =
> (MyFormBean)session.get("formBean");
>
> return "displayFormPage";
> }
>
> public String getMessage()
> {
> return message;
> }
>
> public void setMessage(String message)
> {
> this.message = message;
> }
>
> public String getTarget()
> {
> return target;
> }
>
> public void setTarget(String target)
> {
> this.target = target;
> }
> }
>
>
>
>
> Gabriel Belingueres wrote:
> > Could you post your struts.xml, action and (one of
> the) JSP?
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail:
> user-help@struts.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: session based pojo as form variables
Posted by 928572663 <92...@cox.net>.
I am using Zero Configuration (annotations), so no struts.xml necessary.
----------------
formDetails.jsp
----------------
<!DOCTYPE html PUBLIC "~//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>IPSM struts2 Demo Form</title>
<link href="<s:url value ='/css/main.css'/>" rel="stylesheet"
type="text/css" />
</head>
<body>
<h2>
<s:property value="message" />
</h2>
<s:actionerror />
<s:form action="sample!submit" method="post">
<s:checkbox name="#session.formBean.requery"
label="Requery?" />
<s:textfield name="#session.formBean.firstName"
value="%{#session.formBean.firstName}" label="First Name"
size="16" />
<s:textfield name="#session.formBean.maxValue"
value="%{#session.formBean.maxValue}" label="Max Value"
size="10" /> (<s:property
value="%{#session.formBean.maxValueRangeMin}" />-<s:property
value="%{#session.formBean.maxValueRangeMax}" />)
<s:textfield name="#session.formBean.lastName"
value="%{#session.formBean.lastName}" label="Last Night"
size="10" required="true" />
<s:label key="#session.formBean.maxValueRangeMax"
label="Max Routes (Max)" />
<s:textarea label="Comment" name="#session.formBean.comment"
cols="15" rows="8" />
<s:select name="#session.formBean.selection"
value="%{#session.formBean.selection}"
label="Selection"
list="#session.formBean.picklist" listKey="code"
listValue="label" />
<s:hidden name="target" value="setme" />
<s:url var="back_button_url" value="/images/back_button.gif" />
<s:submit type="image" src="%{#back_button_url}" label=""
cssClass="formButton"
onclick="javascript:this.form.target.value='backButton';
this.form.submit();" />
<s:url var="next_button_url" value="/images/next_button.gif" />
<s:submit type="image" src="%{#next_button_url}" label=""
cssClass="formButton"
onclick="javascript:this.form.target.value='nextButton';
this.form.submit();" />
</s:form>
</body>
</html>
----------------
MyFormBean.jsp
----------------
package sample;
import java.util.ArrayList;
import java.util.List;
public class MyFormBean
{
private boolean requery = true;
private String firstName = "";
private String selection = "";
private String lastName = "";
private String comment = "";
private int maxValue = 0;
//~~~ items for GUI display purposes etc.
private final int maxValueRangeMin = 1;
private final int maxValueRangeMax = 263732;
private List<PicklistItem> picklist = null;
//~~~
public MyFormBean()
{
picklist = new ArrayList<PicklistItem>();
picklist.add(new PicklistItem("1","Item 1"));
picklist.add(new PicklistItem("2","Item 2"));
picklist.add(new PicklistItem("3","Item 3"));
picklist.add(new PicklistItem("4","Item 4"));
picklist.add(new PicklistItem("5","Item 5"));
}
public boolean isRequery()
{
return requery;
}
public void setRequery(boolean requery)
{
this.requery = requery;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getSelection()
{
return selection;
}
public void setSelection(String selection)
{
this.selection = selection;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getComment()
{
return comment;
}
public void setComment(String comment)
{
this.comment = comment;
}
public int getMaxValue()
{
return maxValue;
}
public void setMaxValue(int maxValue)
{
this.maxValue = maxValue;
}
public List<PicklistItem> getPicklist()
{
return picklist;
}
public void setPicklist(List<PicklistItem> picklist)
{
this.picklist = picklist;
}
public int getMaxValueRangeMin()
{
return maxValueRangeMin;
}
public int getMaxValueRangeMax()
{
return maxValueRangeMax;
}
}
----------------
PicklistItem.jsp
----------------
package sample;
public class PicklistItem
{
private String code = "";
private String label = "";
public PicklistItem(String aCode, String aLabel)
{
code = aCode;
label = aLabel;
}
public String getCode()
{
return code;
}
public void setCode(String code)
{
this.code = code;
}
public String getLabel()
{
return label;
}
public void setLabel(String label)
{
this.label = label;
}
}
----------------
SampleAction.jsp
----------------
package sample;
import java.util.Map;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.views.tiles.TilesResult;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Results(
{
@Result(name = "displayFormPage", value = "/formDetails.jsp"),
@Result(name = "homeError", value = "/homeError.page", type =
TilesResult.class)
} )
public class SampleAction extends ActionSupport implements SessionAware
{
private String message = "";
private String target = "";
private MyFormBean formBean = new MyFormBean();
public void setSession(Map<String, Object> session)
{
Object obj = session.get("formBean");
if (obj == null)
{
session.put("formBean", formBean);
}
}
public void setFormBean(MyFormBean formBean)
{
Map session = (Map)ActionContext.getContext().get("session");
session.put("formBean", formBean);
}
public MyFormBean getFormBean()
{
Map session = (Map)ActionContext.getContext().get("session");
return (MyFormBean)session.get("formBean");
}
public String displayForm() throws Exception
{
formBean = new MyFormBean();
formBean.setComment("struts2 is cool");
formBean.setFirstName("Linus");
formBean.setLastName("Torvalds");
formBean.setMaxValue(50);
setFormBean(formBean);
// Map session = (Map) ActionContext.getContext().get("session");
// session.put("formBean",formBean);
return "displayFormPage";
}
public String submit() throws Exception
{
if (target != null
&& target.length() > 0)
{
setMessage(target
+ " was pressed.");
}
// Map session = (Map) ActionContext.getContext().get("session");
// formBean = (MyFormBean)session.get("formBean");
return "displayFormPage";
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
public String getTarget()
{
return target;
}
public void setTarget(String target)
{
this.target = target;
}
}
Gabriel Belingueres wrote:
> Could you post your struts.xml, action and (one of the) JSP?
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: session based pojo as form variables
Posted by Gabriel Belingueres <be...@gmail.com>.
Could you post your struts.xml, action and (one of the) JSP?
2008/9/19 928572663 <92...@cox.net>:
> It's still not working - same error.
>
> I tried to revert my JSP back to using the #session.formBean syntax, and
> now at least I am able to see my default values in the form again.
>
> But when I submit the form with changes, the framework calls my Action's
> setSession() function, but the data values from the form are not copied.
>
> Can someone send me a small JSP and Action class (form with 1 field is
> sufficient) that does actually work?
>
> This sounds like a good idea for a new cookbook page.
>
> Thanks.
>
>
> Gabriel Belingueres wrote:
>>
>> You need to instantiate your formBean before the interceptor stack
>> tries to set the parameters. Common points in your code where you can
>> accomplish that are: instantiating the object when declared as an
>> instance variable, inside a constructor of your action, or implement
>> the Preparable interface and create it inside the prepare() method.
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: session based pojo as form variables
Posted by 928572663 <92...@cox.net>.
It's still not working - same error.
I tried to revert my JSP back to using the #session.formBean syntax,
and now at least I am able to see my default values in the form again.
But when I submit the form with changes, the framework calls my
Action's setSession() function, but the data values from the form are
not copied.
Can someone send me a small JSP and Action class (form with 1 field is
sufficient) that does actually work?
This sounds like a good idea for a new cookbook page.
Thanks.
Gabriel Belingueres wrote:
> You need to instantiate your formBean before the interceptor stack
> tries to set the parameters. Common points in your code where you can
> accomplish that are: instantiating the object when declared as an
> instance variable, inside a constructor of your action, or implement
> the Preparable interface and create it inside the prepare() method.
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: session based pojo as form variables
Posted by Gabriel Belingueres <be...@gmail.com>.
You need to instantiate your formBean before the interceptor stack
tries to set the parameters. Common points in your code where you can
accomplish that are: instantiating the object when declared as an
instance variable, inside a constructor of your action, or implement
the Preparable interface and create it inside the prepare() method.
2008/9/17 928572663 <92...@cox.net>:
> I tried both suggestions and neither worked.
>
> The first thing I tried was to implement the getter/setters and change the
> JSP as you suggested. When I did this I got the following:
>
> Caused by: ognl.OgnlException: formBean [java.lang.NullPointerException]
> at ognl.OgnlRuntime.getMethodValue(OgnlRuntime.java:935)
> at
> ognl.ObjectPropertyAccessor.getPossibleProperty(ObjectPropertyAccessor.java:53)
> at
> ognl.ObjectPropertyAccessor.getProperty(ObjectPropertyAccessor.java:121)
> at
> com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.getProperty(ObjectAccessor.java:17)
> at ognl.OgnlRuntime.getProperty(OgnlRuntime.java:1643)
> at
> com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.getProperty(CompoundRootAccessor.java:116)
> ... 145 more
>
> Then I tried adding the SessionAware interface. The SessionAware interface
> requires me to implement this function:
>
> public void setSession(Map<String, Object> session)
> {
> session.put("formBean", formBean);
> }
>
> however, when the interceptor calls setSession() my formBean object hasn't
> been initialized yet, because the displayForm() action function hasn't been
> called yet. This is the function that creates the pojo form bean.
>
>
>
>
> Gabriel Belingueres wrote:
>>
>> I've never used a form field name like "#session.formBean.firstName"
>> before, but if you refactor your code a little:
>>
>> Make your action implement SessionAware interface and put a formBean
>> getter/setter in your actions (or some superclass of your wizard
>> actions), which takes it from the session:
>> public FormBean getFormBean() {
>> return (FormBean) sessionMap.get("formBean");
>> }
>> public void setFormBean(FormBean f) {
>> sessionMap.put("formBean", f);
>> }
>>
>> then in your JSP page name your form fields like this:
>>
>> <s:textfield name="formBean.firstName" label="First Name" size="16" />
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: session based pojo as form variables
Posted by 928572663 <92...@cox.net>.
I tried both suggestions and neither worked.
The first thing I tried was to implement the getter/setters and change
the JSP as you suggested. When I did this I got the following:
Caused by: ognl.OgnlException: formBean [java.lang.NullPointerException]
at ognl.OgnlRuntime.getMethodValue(OgnlRuntime.java:935)
at
ognl.ObjectPropertyAccessor.getPossibleProperty(ObjectPropertyAccessor.java:53)
at
ognl.ObjectPropertyAccessor.getProperty(ObjectPropertyAccessor.java:121)
at
com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.getProperty(ObjectAccessor.java:17)
at ognl.OgnlRuntime.getProperty(OgnlRuntime.java:1643)
at
com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.getProperty(CompoundRootAccessor.java:116)
... 145 more
Then I tried adding the SessionAware interface. The SessionAware
interface requires me to implement this function:
public void setSession(Map<String, Object> session)
{
session.put("formBean", formBean);
}
however, when the interceptor calls setSession() my formBean object
hasn't been initialized yet, because the displayForm() action function
hasn't been called yet. This is the function that creates the pojo form
bean.
Gabriel Belingueres wrote:
> I've never used a form field name like "#session.formBean.firstName"
> before, but if you refactor your code a little:
>
> Make your action implement SessionAware interface and put a formBean
> getter/setter in your actions (or some superclass of your wizard
> actions), which takes it from the session:
> public FormBean getFormBean() {
> return (FormBean) sessionMap.get("formBean");
> }
> public void setFormBean(FormBean f) {
> sessionMap.put("formBean", f);
> }
>
> then in your JSP page name your form fields like this:
>
> <s:textfield name="formBean.firstName" label="First Name" size="16" />
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: session based pojo as form variables
Posted by Gabriel Belingueres <be...@gmail.com>.
I've never used a form field name like "#session.formBean.firstName"
before, but if you refactor your code a little:
Make your action implement SessionAware interface and put a formBean
getter/setter in your actions (or some superclass of your wizard
actions), which takes it from the session:
public FormBean getFormBean() {
return (FormBean) sessionMap.get("formBean");
}
public void setFormBean(FormBean f) {
sessionMap.put("formBean", f);
}
then in your JSP page name your form fields like this:
<s:textfield name="formBean.firstName" label="First Name" size="16" />
2008/9/17 928572663 <92...@cox.net>:
> Here are some examples of how I am accessing the session pojo fields on
> my form:
>
> <s:form action="Sample!submit" method="post" >
> <s:checkbox name="#session.formBean.requeryInfo" label="Requery
> Info?" />
>
> <s:textfield name="#session.formBean.firstName"
> value="%{#session.formBean.firstName}" label="First Name" size="16" />
>
> <!-- etc -->
> </s:form>
>
>
> and then in my action class (SampleAction):
>
> public String showForm() throws Exception
> {
> MyFormBean formBean = new MyFormBean();
>
> Map session = (Map)ActionContext.getContext().get("session");
> session.put("formBean",formBean);
>
> return ("showFormPage");
> }
>
> public String submit() throws Exception
> {
> Map session = (Map)ActionContext.getContext().get("session");
> MyFormBean formBean = (MyFormBean)session.get("formBean");
>
> formBean.setMessage("submit was received and processed.");
>
> return "showFormPage";
> }
>
> So when I set a break point in submit(), I can see that it is invoked,
> but when I pull the "formBean" out of session I find that it is still
> the unedited copy (original). Struts2 apparently has not copied the
> form value into the pojo valueobj for me.
>
> BTW - I am using "zero configuration" and struts2 2.1.2
>
> Thanks.
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: session based pojo as form variables
Posted by 928572663 <92...@cox.net>.
Here are some examples of how I am accessing the session pojo fields on
my form:
<s:form action="Sample!submit" method="post" >
<s:checkbox name="#session.formBean.requeryInfo" label="Requery
Info?" />
<s:textfield name="#session.formBean.firstName"
value="%{#session.formBean.firstName}" label="First Name" size="16" />
<!-- etc -->
</s:form>
and then in my action class (SampleAction):
public String showForm() throws Exception
{
MyFormBean formBean = new MyFormBean();
Map session = (Map)ActionContext.getContext().get("session");
session.put("formBean",formBean);
return ("showFormPage");
}
public String submit() throws Exception
{
Map session = (Map)ActionContext.getContext().get("session");
MyFormBean formBean = (MyFormBean)session.get("formBean");
formBean.setMessage("submit was received and processed.");
return "showFormPage";
}
So when I set a break point in submit(), I can see that it is invoked,
but when I pull the "formBean" out of session I find that it is still
the unedited copy (original). Struts2 apparently has not copied the
form value into the pojo valueobj for me.
BTW - I am using "zero configuration" and struts2 2.1.2
Thanks.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: session based pojo as form variables
Posted by Dave Newton <ne...@yahoo.com>.
--- On Tue, 9/16/08, John Norvell wrote:
> Is there simple answer to this? I can include some code
> snippets if necessary.
Necessary ;)
There's no way to know what your form is storing its values to w/o seeing the action and what it's exposing to the JSP.
Dave
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org