You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Dimitrios Christodoulakis <di...@gmail.com> on 2009/06/06 02:52:14 UTC

trouble retrieving a request parameter with struts2 tags

Hello,

I am building a struts2-hibernate-mysql application. A user after
navigating through some pages reaches a pages where all the records on
the database are listed and can perform crud operations.

The jsp which iterates and displays the records is:

<%@ taglib prefix="s" uri="/struts-tags"%>

<p>Persons found on database</p>
<a href="<s:url action='adduser'/>">Add new User</a>
<s:if test="persons.size > 0">	
	<table>
		<s:iterator value="persons">
			<tr id="row_<s:property value="id"/>">
				
				<td>
					<s:property value="id" />
				</td>
				<td>
					<s:property value="firstName" />
				</td>
				<td>
					<s:property value="lastName" />
				</td>
				<td>
						<s:url action="remove" var="remURL">
							<s:param name="id" value="id" />
						</s:url>
				<a href="<s:property value ="remURL"/>">Remove</a>	
				</td>
				<td>
						<s:url action="edit" var="editURL">
							<s:param name="id" value="id" />
						</s:url>
					<a href="<s:property value ="editURL"/>">Edit</a>
				</td>
				<td>
					<a style="color: #FF0000" href="Accounts">View account details</a>
				</td>
			</tr>
		</s:iterator>
	</table>
</s:if>
<a href="<s:url action='admin'/>">Back to Administrator's homepage</a>

---As you can see within the above jsp an "edit" link is generated for
every record and the record id is passed as a parameter to the URL,
with: <s:param name="id" value="id" />

When the user follows the "edit" link, the URL forms like this:

http://localhost/MyApp/edit.action?id=38

The record identifier is passed as a parameter in the URL: ?id=38

The link brings the user to a new form to perform a simple edit of the
record; first name and last name:

The jsp with the edit form is the following:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

	<head>
    <title>Update User</title>
    <s:head/>
	</head>

	<body>
		<h4>Edit information for selected user</h4>
		<s:form action="save">   	
     	  <s:textfield name="person.firstName"
value="%{person.firstName}" label="First Name"/>
    	  <s:textfield name="person.lastName" label="Last Name"/>
    	  <s:hidden name="person.id" value="#attr.id" label="Primary Key" />
    	  <s:submit/>
		</s:form>	
		
	</body>
	
</html>

The aim is to have the hidden field pre-filled with the value taken
from the URL parameter as soon as the user clicks on the "edit" link
and reaches the new form to update a record. Unfortunately so far I
have not been able to retrieve that parameter and use its value to
fill that texfield. The id value is always null.

The consequence is that the code performs a new insert instead of an
update, it sees the null id and assumes a new record must be inserted.
Could anyone perhaps provide some advice how can I solve this?

If required my struts.xml file is:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.devMode" value="false"/>

    <package name="default" namespace="/" extends="struts-default">
        <action name="Menu">
            <result>pages/firstpage.jsp</result>
        </action>

         <action name="user">
            <result>pages/user/user_auth.jsp</result>
        </action>

        <action name="admin">
            <result>pages/admin/admin_auth.jsp</result>
        </action>

        <action name="list" method="execute" class="org.user.PersonAction">
            	<result>/pages/list.jsp</result>
            	<result name="input">pages/list.jsp</result>
        </action>

        <action name="remove" method="remove" class="org.user.PersonAction" >
            <result>/pages/list.jsp</result>
            <result name="input">pages/list.jsp</result>
        </action>

        <action name="adduser">
            <result>pages/user/Registration.jsp</result>
        </action>

        <action name="save" method="save" class="org.user.PersonAction" >
            <result>/pages/list.jsp</result>
            <result name="SUCCESS">pages/list.jsp</result>
        </action>

        <action name="edit">
            <result>pages/user/UpdateAccountForm.jsp</result>
        </action>



    </package>
    <include file="org/visitor/visitor.xml"/>

</struts>

-----And the action class is:

package org.user;

import java.util.List;
import java.util.Map;

//import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ParameterAware;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.Action;

public class PersonAction implements Preparable, ParameterAware{
	
	private PersonService service = new PersonServiceImpl();
    private List<Person> persons;
    private Person person;
    private Integer id;
    private Map parameters;



    public PersonAction(){}

    public PersonAction(PersonService service) {
        this.service = service;
    }

    public String execute() {
    	System.out.println("Inside execute method");
        this.persons = service.findAll();
        return Action.SUCCESS;
    }

    public String save() {
    //	HttpServletRequest request = (HttpServletRequest)
getServletContext.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");
    	 Map map = getParameters();
         id = (Integer)map.get("id");
         System.out.println("In PersonAction rertieving request param "+id );
    	//	parameters = this.getParameters();
    //	id = (Integer) parameters.get(id);
   // 	person.setId(id);
    	System.out.println("In PersonAction before service.save");
        this.service.save(person);
        System.out.println("In PersonAction after service.save");
        System.out.println("In PersionAction" + " " +
person.getFirstName()+" "+person.getLastName()+" "+person.getId());
        return execute();
    }

    public String remove() {
        service.remove(id);
        return execute();
    }

    public List<Person> getPersons() {
        return persons;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void prepare() throws Exception {
        if (id != null)
            person = service.find(id);
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
    public void setParameters(Map parameters) {
    	      this.parameters=parameters;
    	     }
    public Map getParameters() {
    	      return parameters;
    	     }


}


---- I have also tried unsuccessfully to retrieve the parameter value
from the action code, but always get null. Also the delegate class
implementing the actual action methods is the following:

package org.user;

import java.util.List;

import org.hibernate.*;
import org.persist.util.*;

//import org.springframework.orm.hibernate3.support.OpenSessionInViewFilter;
//import org.springframework.web.context.ContextLoaderListener;
//import org.hibernate.transaction.JDBCTransactionFactory;

public class PersonServiceImpl implements PersonService {
	
	Session session;

    public PersonServiceImpl() {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

	
	public void setSession(Session s){
		this.session = s;
	}
	public Session getSession(){
		if (session == null){
			session = HibernateUtil.getSession();
		}
	return session;
	}

	@Override
	public void clear() {
		getSession().clear();
		
	}

	@Override
	public Person find(int id) {
		try {
			getSession().beginTransaction();
			Query q = session.createQuery("from Person p where p.id=:id");
			q.setInteger("id",id);
			return  (Person)q.uniqueResult();
			}
		finally {
			//getSession().close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Person> findAll() {
		try {
			Session session = HibernateUtil.getSession();
			session.beginTransaction();
			Query q = session.createQuery("select p FROM Person p");
			return q.list();
		}
		catch(Exception e){
			System.out.print("Error while fetching "+e);
			return null;
		}
		finally	{
			//session.close();
		}
	}

	@Override
	public void flush() {
		getSession().flush();		
	}

	@Override
	public void remove(int id) {
		Person person = find(id);
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		if (person != null){
			try {
				tx = session.beginTransaction();
				person =(Person)session.load(Person.class,id);
				session.delete(person);
				tx.commit();
			}catch (RuntimeException e) {
				if(tx != null) tx.rollback();
				throw e;
			} finally {
				session.close();
			}
		}
		
	}

	@Override
	public void save(Person person) {
		 Session session = HibernateUtil.getSession();
		 Transaction tx = session.beginTransaction();
		     try {  		
		         if (person.getId() == null) {
		        	 System.out.println("id = null, inside if block");
		             session.persist(person);
		             System.out.println(person.getFirstName()+"
"+person.getLastName()+" "+person.getId());
		         } else {
		        	 System.out.println("id not null, inside else block");
		             session.merge(person);
		             System.out.println(person.getFirstName()+"
"+person.getLastName()+" "+person.getId());
		         }
		        tx.commit();
		    } catch (RuntimeException e) {
		         if (tx.isActive()) tx.rollback();
		         throw e;
		     }
		
		/*Session session = HibernateUtil.getSession();
		System.out.println("1");
		Transaction tx=null;
		System.out.println("2");
		if (person.getId() == null) {
			System.out.println("3");
			System.out.println(person.getFirstName()+" "+person.getLastName()+"
"+person.getId());
			try {
				tx = session.beginTransaction();
				session.persist(person);
				System.out.println("4");
				tx.commit();
				System.out.println("5");
			} catch (RuntimeException e) {
				if(tx != null) tx.rollback();
				System.out.println("6");
				throw e;
			}
			
        } else {
        	System.out.println("7");
        	try {
				tx = session.beginTransaction();
				System.out.println("8");
				session.merge(person);
				System.out.println("9");
				System.out.println(person.getFirstName()+"
"+person.getLastName()+" "+person.getId());
				tx.commit();
			} catch (RuntimeException e) {
				if(tx != null) tx.rollback();
				System.out.println("10");
				throw e;
			}        	

        }*/

		
		
		/*try {
			tx = session.beginTransaction();
			session.saveOrUpdate(person);
			tx.commit();
		} catch (RuntimeException e) {
			if(tx != null) tx.rollback();
			throw e;
		} finally {
			session.close();
		}*/
		
	}

}


---- Thank you in advance for any help on this situation.
Regards to everyone!

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: trouble retrieving a request parameter with struts2 tags

Posted by Dave Newton <ne...@yahoo.com>.
Dimitrios Christodoulakis wrote:
> A further look at the struts 2 tag reference:
> http://struts.apache.org/2.1.6/docs/url.html
> Doesn't indicate that the url tag has such an attribute -- I am using
> struts 2 version 2.1.6, was that attribute used in other versions?

No. I assumed an attempt at humor, but was just confusing instead.

> But, you made a good point, are there other ways to escape special
> characters with attributes, or further manual coding are needed?

AFAIK <s:url> and <s:property> will encode things in an HTML-reasonable 
way by default. <s:property> adds the "escapeJavaScript" property for 
use in JS expressions.

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: trouble retrieving a request parameter with struts2 tags

Posted by Dimitrios Christodoulakis <di...@gmail.com>.
That is interesting. I tried using the escapeTick="true" attribute
with the s:url, but got an exception:

org.apache.jasper.JasperException: /pages/list.jsp(26,6) Attribute
escapeTick invalid for tag url according to TLD

A further look at the struts 2 tag reference:
http://struts.apache.org/2.1.6/docs/url.html
Doesn't indicate that the url tag has such an attribute -- I am using
struts 2 version 2.1.6, was that attribute used in other versions?

Since you mentioned it, I tried an example, without the escapeTick
attribute and the URL now forms like this:
http://localhost/MyApp/edit.action?id=69&firstName=colin&lastName=o%27hara

Although the tick (or apostrophe) is not escaped, when I retrieve the
lastName parameter to have the field pre-populated, the name is
rendered correctly.

But, you made a good point, are there other ways to escape special
characters with attributes, or further manual coding are needed?

On Sat, Jun 6, 2009 at 10:26 PM, Martin Gainty<mg...@hotmail.com> wrote:
>
> yes that works for john doe
> but those of us with irish names like patrick o'donnell would need the tick mark escaped
>  <s:url action="edit" var="editURL" escapeAmp = "false" escapeTick="true">
> ?
> Martin Gainty
> ______________________________________________
> Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
>
> Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
> Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.
>
>
>
>
>> Date: Sat, 6 Jun 2009 13:13:08 -0500
>> Subject: Re: trouble retrieving a request parameter with struts2 tags
>> From: dimi.chr@gmail.com
>> To: user@struts.apache.org
>>
>> With a little bit more searching I found the escapeAmp attribute of
>> the <s:url> tag. That did the trick:
>>
>> <s:url action="edit" var="editURL" escapeAmp = "false">
>>
>> Thanks!
>>
>> On Sat, Jun 6, 2009 at 10:41 AM, Dimitrios
>> Christodoulakis<di...@gmail.com> wrote:
>> > Thank you for the information. That ofnl expression actually worked.
>> > The textfield is populated with the value taken from the URL
>> > parameter.
>> >
>> > But something strange happens when I try to put more parameters on the request:
>> >
>> > <s:url action="edit" var="editURL">
>> >        <s:param name="id" value="%{id}" />
>> >        <s:param name="firstName" value="%{firstName}" />
>> >        <s:param name="lastName" value="%{lastName}" />
>> > </s:url>
>> >
>> > So the link forms like this:
>> >
>> > http://localhost/MyApp/edit.action?id=38&firstName=john&lastName=doe
>> >
>> > Next, on the update page, I try to retrieve not only the id, but the
>> > first name and last name too from the URL (in order to pre-fill the
>> > textfields):
>> >
>> > <s:form action="save">
>> >          <s:textfield name="person.firstName"
>> > value="%{#parameters.firstName}" label="First Name"/>
>> >          <s:textfield name="person.lastName"
>> > value="%{#parameters.lastName}" label="Last Name"/>
>> >          <s:hidden name="person.id" value="%{#parameters.id}"
>> > label="Primary Key" />
>> >          <s:submit/>
>> > </s:form>
>> >
>> > Although ognl correctly picks up the first parameter (id), takes its
>> > value and populates the hidden field, I get 2 ognl warnings: one for
>> > the first name and one for the last name:
>> >
>> > WARN OgnlValueStack:49 - Error setting value
>> > ognl.ExpressionSyntaxException: Malformed OGNL expression:
>> > amp;firstName [ognl.TokenMgrError: Lexical error at line 1, column 4.
>> > Encountered: ";" (59), after : ""]
>> >
>> > WARN OgnlValueStack:49 - Error setting value
>> > ognl.ExpressionSyntaxException: Malformed OGNL expression:
>> > amp;lastName [ognl.TokenMgrError: Lexical error at line 1, column 4.
>> > Encountered: ";" (59), after : ""]
>> >
>> > A look at how the URL forms confirms this: Instead of adding the
>> > parameters in URL using just a "&", parameters are separated by this:
>> > "&"
>> >
>> > --Is that some sort of bug when evaluating multiple struts2 param tags
>> > with ognl, or do I have something wrong in the jsp code?
>> > The actual action ode works ok, I believe the issue is within that jsp only.
>> >
>> > Again, I appreciate any further advice on this.
>> > Regards to everyone.
>> >
>> >
>> >
>> > On Fri, Jun 5, 2009 at 9:32 PM, Dave Newton<ne...@yahoo.com> wrote:
>> >> Dimitrios Christodoulakis wrote:
>> >>>
>> >>> <s:hidden name="person.id" value="#attr.id" label="Primary Key" />
>> >>
>> >> I told you #attr.id on javaranch; I meant #parameters. This information is
>> >> readily available via the S2 docs. [1, 2]
>> >>
>> >> IIRC the "value" attribute expects a literal; I'd wrap it in the OGNL
>> >> escape, %{#parameters.id}, to make sure it's evaluated. Correct rendering
>> >> is, of course, trivially verifiable by viewing the rendered source.
>> >>
>> >> The fact remains that if person.id isn't being filled correctly by your Java
>> >> code something is still wrong with it.
>> >>
>> >>> I have also tried unsuccessfully to retrieve the parameter value
>> >>> from the action code, but always get null.
>> >>
>> >> If I'm understanding what you're saying there then something is doubly wrong
>> >> with either your config or your assumptions: there are several ways to
>> >> retrieve URL parameters, and AFAIK they all work.
>> >>
>> >> Note that the result named "SUCCESS" will never be used if you're using the
>> >> ActionSupport convenience result constants.
>> >>
>> >> Dave
>> >>
>> >> [1] OGNL
>> >> http://struts.apache.org/2.x/docs/ognl.html
>> >>
>> >> [2] OGNL Basics
>> >> http://struts.apache.org/2.x/docs/ognl-basics.html
>> >>
>> >> ---------------------------------------------------------------------
>> >> 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
>>
>
> _________________________________________________________________
> Windows Live™: Keep your life in sync.
> http://windowslive.com/explore?ocid=TXT_TAGLM_WL_BR_life_in_synch_062009

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: trouble retrieving a request parameter with struts2 tags

Posted by Martin Gainty <mg...@hotmail.com>.
yes that works for john doe
but those of us with irish names like patrick o'donnell would need the tick mark escaped
 <s:url action="edit" var="editURL" escapeAmp = "false" escapeTick="true">
?
Martin Gainty 
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




> Date: Sat, 6 Jun 2009 13:13:08 -0500
> Subject: Re: trouble retrieving a request parameter with struts2 tags
> From: dimi.chr@gmail.com
> To: user@struts.apache.org
> 
> With a little bit more searching I found the escapeAmp attribute of
> the <s:url> tag. That did the trick:
> 
> <s:url action="edit" var="editURL" escapeAmp = "false">
> 
> Thanks!
> 
> On Sat, Jun 6, 2009 at 10:41 AM, Dimitrios
> Christodoulakis<di...@gmail.com> wrote:
> > Thank you for the information. That ofnl expression actually worked.
> > The textfield is populated with the value taken from the URL
> > parameter.
> >
> > But something strange happens when I try to put more parameters on the request:
> >
> > <s:url action="edit" var="editURL">
> >        <s:param name="id" value="%{id}" />
> >        <s:param name="firstName" value="%{firstName}" />
> >        <s:param name="lastName" value="%{lastName}" />
> > </s:url>
> >
> > So the link forms like this:
> >
> > http://localhost/MyApp/edit.action?id=38&amp;firstName=john&amp;lastName=doe
> >
> > Next, on the update page, I try to retrieve not only the id, but the
> > first name and last name too from the URL (in order to pre-fill the
> > textfields):
> >
> > <s:form action="save">
> >          <s:textfield name="person.firstName"
> > value="%{#parameters.firstName}" label="First Name"/>
> >          <s:textfield name="person.lastName"
> > value="%{#parameters.lastName}" label="Last Name"/>
> >          <s:hidden name="person.id" value="%{#parameters.id}"
> > label="Primary Key" />
> >          <s:submit/>
> > </s:form>
> >
> > Although ognl correctly picks up the first parameter (id), takes its
> > value and populates the hidden field, I get 2 ognl warnings: one for
> > the first name and one for the last name:
> >
> > WARN OgnlValueStack:49 - Error setting value
> > ognl.ExpressionSyntaxException: Malformed OGNL expression:
> > amp;firstName [ognl.TokenMgrError: Lexical error at line 1, column 4.
> > Encountered: ";" (59), after : ""]
> >
> > WARN OgnlValueStack:49 - Error setting value
> > ognl.ExpressionSyntaxException: Malformed OGNL expression:
> > amp;lastName [ognl.TokenMgrError: Lexical error at line 1, column 4.
> > Encountered: ";" (59), after : ""]
> >
> > A look at how the URL forms confirms this: Instead of adding the
> > parameters in URL using just a "&", parameters are separated by this:
> > "&amp;"
> >
> > --Is that some sort of bug when evaluating multiple struts2 param tags
> > with ognl, or do I have something wrong in the jsp code?
> > The actual action ode works ok, I believe the issue is within that jsp only.
> >
> > Again, I appreciate any further advice on this.
> > Regards to everyone.
> >
> >
> >
> > On Fri, Jun 5, 2009 at 9:32 PM, Dave Newton<ne...@yahoo.com> wrote:
> >> Dimitrios Christodoulakis wrote:
> >>>
> >>> <s:hidden name="person.id" value="#attr.id" label="Primary Key" />
> >>
> >> I told you #attr.id on javaranch; I meant #parameters. This information is
> >> readily available via the S2 docs. [1, 2]
> >>
> >> IIRC the "value" attribute expects a literal; I'd wrap it in the OGNL
> >> escape, %{#parameters.id}, to make sure it's evaluated. Correct rendering
> >> is, of course, trivially verifiable by viewing the rendered source.
> >>
> >> The fact remains that if person.id isn't being filled correctly by your Java
> >> code something is still wrong with it.
> >>
> >>> I have also tried unsuccessfully to retrieve the parameter value
> >>> from the action code, but always get null.
> >>
> >> If I'm understanding what you're saying there then something is doubly wrong
> >> with either your config or your assumptions: there are several ways to
> >> retrieve URL parameters, and AFAIK they all work.
> >>
> >> Note that the result named "SUCCESS" will never be used if you're using the
> >> ActionSupport convenience result constants.
> >>
> >> Dave
> >>
> >> [1] OGNL
> >> http://struts.apache.org/2.x/docs/ognl.html
> >>
> >> [2] OGNL Basics
> >> http://struts.apache.org/2.x/docs/ognl-basics.html
> >>
> >> ---------------------------------------------------------------------
> >> 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
> 

_________________________________________________________________
Windows Live™: Keep your life in sync. 
http://windowslive.com/explore?ocid=TXT_TAGLM_WL_BR_life_in_synch_062009

Re: trouble retrieving a request parameter with struts2 tags

Posted by Dimitrios Christodoulakis <di...@gmail.com>.
With a little bit more searching I found the escapeAmp attribute of
the <s:url> tag. That did the trick:

<s:url action="edit" var="editURL" escapeAmp = "false">

Thanks!

On Sat, Jun 6, 2009 at 10:41 AM, Dimitrios
Christodoulakis<di...@gmail.com> wrote:
> Thank you for the information. That ofnl expression actually worked.
> The textfield is populated with the value taken from the URL
> parameter.
>
> But something strange happens when I try to put more parameters on the request:
>
> <s:url action="edit" var="editURL">
>        <s:param name="id" value="%{id}" />
>        <s:param name="firstName" value="%{firstName}" />
>        <s:param name="lastName" value="%{lastName}" />
> </s:url>
>
> So the link forms like this:
>
> http://localhost/MyApp/edit.action?id=38&amp;firstName=john&amp;lastName=doe
>
> Next, on the update page, I try to retrieve not only the id, but the
> first name and last name too from the URL (in order to pre-fill the
> textfields):
>
> <s:form action="save">
>          <s:textfield name="person.firstName"
> value="%{#parameters.firstName}" label="First Name"/>
>          <s:textfield name="person.lastName"
> value="%{#parameters.lastName}" label="Last Name"/>
>          <s:hidden name="person.id" value="%{#parameters.id}"
> label="Primary Key" />
>          <s:submit/>
> </s:form>
>
> Although ognl correctly picks up the first parameter (id), takes its
> value and populates the hidden field, I get 2 ognl warnings: one for
> the first name and one for the last name:
>
> WARN OgnlValueStack:49 - Error setting value
> ognl.ExpressionSyntaxException: Malformed OGNL expression:
> amp;firstName [ognl.TokenMgrError: Lexical error at line 1, column 4.
> Encountered: ";" (59), after : ""]
>
> WARN OgnlValueStack:49 - Error setting value
> ognl.ExpressionSyntaxException: Malformed OGNL expression:
> amp;lastName [ognl.TokenMgrError: Lexical error at line 1, column 4.
> Encountered: ";" (59), after : ""]
>
> A look at how the URL forms confirms this: Instead of adding the
> parameters in URL using just a "&", parameters are separated by this:
> "&amp;"
>
> --Is that some sort of bug when evaluating multiple struts2 param tags
> with ognl, or do I have something wrong in the jsp code?
> The actual action ode works ok, I believe the issue is within that jsp only.
>
> Again, I appreciate any further advice on this.
> Regards to everyone.
>
>
>
> On Fri, Jun 5, 2009 at 9:32 PM, Dave Newton<ne...@yahoo.com> wrote:
>> Dimitrios Christodoulakis wrote:
>>>
>>> <s:hidden name="person.id" value="#attr.id" label="Primary Key" />
>>
>> I told you #attr.id on javaranch; I meant #parameters. This information is
>> readily available via the S2 docs. [1, 2]
>>
>> IIRC the "value" attribute expects a literal; I'd wrap it in the OGNL
>> escape, %{#parameters.id}, to make sure it's evaluated. Correct rendering
>> is, of course, trivially verifiable by viewing the rendered source.
>>
>> The fact remains that if person.id isn't being filled correctly by your Java
>> code something is still wrong with it.
>>
>>> I have also tried unsuccessfully to retrieve the parameter value
>>> from the action code, but always get null.
>>
>> If I'm understanding what you're saying there then something is doubly wrong
>> with either your config or your assumptions: there are several ways to
>> retrieve URL parameters, and AFAIK they all work.
>>
>> Note that the result named "SUCCESS" will never be used if you're using the
>> ActionSupport convenience result constants.
>>
>> Dave
>>
>> [1] OGNL
>> http://struts.apache.org/2.x/docs/ognl.html
>>
>> [2] OGNL Basics
>> http://struts.apache.org/2.x/docs/ognl-basics.html
>>
>> ---------------------------------------------------------------------
>> 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: trouble retrieving a request parameter with struts2 tags

Posted by Dimitrios Christodoulakis <di...@gmail.com>.
Thank you for the information. That ofnl expression actually worked.
The textfield is populated with the value taken from the URL
parameter.

But something strange happens when I try to put more parameters on the request:

<s:url action="edit" var="editURL">
	<s:param name="id" value="%{id}" />
	<s:param name="firstName" value="%{firstName}" />
	<s:param name="lastName" value="%{lastName}" />
</s:url>

So the link forms like this:

http://localhost/MyApp/edit.action?id=38&amp;firstName=john&amp;lastName=doe

Next, on the update page, I try to retrieve not only the id, but the
first name and last name too from the URL (in order to pre-fill the
textfields):

<s:form action="save">   	
     	  <s:textfield name="person.firstName"
value="%{#parameters.firstName}" label="First Name"/>
    	  <s:textfield name="person.lastName"
value="%{#parameters.lastName}" label="Last Name"/>
    	  <s:hidden name="person.id" value="%{#parameters.id}"
label="Primary Key" />
    	  <s:submit/>
</s:form>	

Although ognl correctly picks up the first parameter (id), takes its
value and populates the hidden field, I get 2 ognl warnings: one for
the first name and one for the last name:

WARN OgnlValueStack:49 - Error setting value
ognl.ExpressionSyntaxException: Malformed OGNL expression:
amp;firstName [ognl.TokenMgrError: Lexical error at line 1, column 4.
Encountered: ";" (59), after : ""]

WARN OgnlValueStack:49 - Error setting value
ognl.ExpressionSyntaxException: Malformed OGNL expression:
amp;lastName [ognl.TokenMgrError: Lexical error at line 1, column 4.
Encountered: ";" (59), after : ""]

A look at how the URL forms confirms this: Instead of adding the
parameters in URL using just a "&", parameters are separated by this:
"&amp;"

--Is that some sort of bug when evaluating multiple struts2 param tags
with ognl, or do I have something wrong in the jsp code?
The actual action ode works ok, I believe the issue is within that jsp only.

Again, I appreciate any further advice on this.
Regards to everyone.



On Fri, Jun 5, 2009 at 9:32 PM, Dave Newton<ne...@yahoo.com> wrote:
> Dimitrios Christodoulakis wrote:
>>
>> <s:hidden name="person.id" value="#attr.id" label="Primary Key" />
>
> I told you #attr.id on javaranch; I meant #parameters. This information is
> readily available via the S2 docs. [1, 2]
>
> IIRC the "value" attribute expects a literal; I'd wrap it in the OGNL
> escape, %{#parameters.id}, to make sure it's evaluated. Correct rendering
> is, of course, trivially verifiable by viewing the rendered source.
>
> The fact remains that if person.id isn't being filled correctly by your Java
> code something is still wrong with it.
>
>> I have also tried unsuccessfully to retrieve the parameter value
>> from the action code, but always get null.
>
> If I'm understanding what you're saying there then something is doubly wrong
> with either your config or your assumptions: there are several ways to
> retrieve URL parameters, and AFAIK they all work.
>
> Note that the result named "SUCCESS" will never be used if you're using the
> ActionSupport convenience result constants.
>
> Dave
>
> [1] OGNL
> http://struts.apache.org/2.x/docs/ognl.html
>
> [2] OGNL Basics
> http://struts.apache.org/2.x/docs/ognl-basics.html
>
> ---------------------------------------------------------------------
> 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: trouble retrieving a request parameter with struts2 tags

Posted by Dave Newton <ne...@yahoo.com>.
Dimitrios Christodoulakis wrote:
> <s:hidden name="person.id" value="#attr.id" label="Primary Key" />

I told you #attr.id on javaranch; I meant #parameters. This information 
is readily available via the S2 docs. [1, 2]

IIRC the "value" attribute expects a literal; I'd wrap it in the OGNL 
escape, %{#parameters.id}, to make sure it's evaluated. Correct 
rendering is, of course, trivially verifiable by viewing the rendered 
source.

The fact remains that if person.id isn't being filled correctly by your 
Java code something is still wrong with it.

> I have also tried unsuccessfully to retrieve the parameter value
> from the action code, but always get null.

If I'm understanding what you're saying there then something is doubly 
wrong with either your config or your assumptions: there are several 
ways to retrieve URL parameters, and AFAIK they all work.

Note that the result named "SUCCESS" will never be used if you're using 
the ActionSupport convenience result constants.

Dave

[1] OGNL
http://struts.apache.org/2.x/docs/ognl.html

[2] OGNL Basics
http://struts.apache.org/2.x/docs/ognl-basics.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org