struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gabriel Belingueres" <belingue...@gmail.com>
Subject Re: session based pojo as form variables
Date Sat, 20 Sep 2008 08:41:56 GMT
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 <newton.dave@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 <928572663@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


Mime
View raw message