You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by thogau <th...@thogau.net> on 2008/02/21 18:39:28 UTC

dependant updated with ajax : how?

Hi,

I know this has been asked several times on this list but I couldn't find
what I need...

I am trying to do something that seems very basic to me, at least it was
with struts 1.x and DWR. 
Since struts 2 is shipped with dojo, I would like to use this framework only
(no DWR, nor JWP, ...). doubleselect is not an option for me since I
actually need 3 dependant select tags.

How can I have 2 select tags where the content of the second one depends on
the selected option in the first one. Of course I would like to do it ajax
flavoured (no page reload).

Has anybody an example of this running smoothly and willing to share?
Any pointer would be warmly welcome cause I didn't expect to get into
troubles implementing such a basic ajax behaviour...

Thanks.


-- 
View this message in context: http://www.nabble.com/dependant-%3Cs%3Aselect%3E-updated-with-ajax-%3A-how--tp15616529p15616529.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: dependant updated with ajax : how?

Posted by thogau <th...@thogau.net>.
I managed to have it working, sources are below. By the way, I do not
understand why the selected values in the combos are not submited (I have to
use some input hidden in the form...) any tip?
Hope it helps.

@Musachy : thanks for the link but I have problems to use most of the 2.1
examples.
@Dave : i'm struts 2 noob and still don't figure out clearly how to adapt a
pure dojo example to struts-dojo.

struts.xml
==============
<action name="handleSelectActionAjax"
class="org.rca.conticabase.web.action.MultiSelectAction"
method="handleSelect">
	<result>/WEB-INF/components/multiSelect.jsp</result>
</action>
<action name="submitSelectAction"
class="org.rca.conticabase.web.action.MultiSelectAction"
method="handleSubmit">
	<result>/WEB-INF/pages/ajaxPage.jsp</result>
</action>
==============

MultiSelectAction.java
==============
public class MultiSelectAction extends ActionSupport {

	private static final long serialVersionUID = -4617365382177316987L;

	private String firstSelectValue;
	private String secondSelectValue;
	private String thirdSelectValue;

	private List<String> secondList;
	private List<String> thirdList;

	private String message;
	
	public String getFirstSelectValue() {
		return firstSelectValue;
	}

	public void setFirstSelectValue(String s) {
		this.firstSelectValue = s;
	}

	public List<String> getSecondList() {
		return secondList;
	}

	public void setSecondList(List<String> l) {
		this.secondList = l;
	}

	public String getSecondSelectValue() {
		return secondSelectValue;
	}

	public void setSecondSelectValue(String s) {
		this.secondSelectValue = s;
	}

	public List<String> getThirdList() {
		return thirdList;
	}

	public void setThirdList(List<String> l) {
		this.thirdList = l;
	}

	public String getThirdSelectValue() {
		return thirdSelectValue;
	}

	public void setThirdSelectValue(String s) {
		this.thirdSelectValue = s;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String handleSelect() throws Exception{
		if (ServletActionContext.getServletContext().getAttribute("firstList") ==
null){
			ArrayList<String> firstList = new ArrayList<String>();
			firstList.add("1");
			firstList.add("2");
			firstList.add("3");
			ServletActionContext.getServletContext().setAttribute("firstList",
firstList);
		}
		if (getFirstSelectValue() != null && !getFirstSelectValue().equals("")) {
			secondList = new ArrayList<String>();
			secondList.add(getFirstSelectValue() + "-1");
			secondList.add(getFirstSelectValue() + "-2");
		}
		if (getSecondSelectValue() != null && !getSecondSelectValue().equals(""))
{
			thirdList = new ArrayList<String>();
			thirdList.add(getSecondSelectValue() + "-1");
			thirdList.add(getSecondSelectValue() + "-2");
		}
		return SUCCESS;		
	}
	
	public String handleSubmit() throws Exception{
		message = "your choice: '" + firstSelectValue + "'"
		+ ", '" + secondSelectValue + "'"
		+ ", '" + thirdSelectValue + "'";
		return SUCCESS;		
	}
}
==============

multiSelect.jsp
==============
<%@ include file="/common/taglibs.jsp"%>

<s:if test="%{#application.firstList != null}">
	<s:select label="Level 1" id="firstSelect" list="%{#application.firstList}" 
		emptyOption="true" cssStyle="width: 100px;"
		onchange="handleSelect(this);" value="firstSelectValue"/>
</s:if>
<s:if test="secondList != null">
	<s:select label="Level 2" id="secondSelect" list="secondList" 
		emptyOption="true"  cssStyle="width: 100px;"
		onchange="handleSelect(this);" value="secondSelectValue"/>
</s:if>
<s:if test="thirdList != null">
	<s:select label="Level 3" id="thirdSelect" list="thirdList" 
		emptyOption=" true" cssStyle="width: 100px;"
		value="thirdSelectValue"/>
</s:if>
==============

ajaxPage.jsp
==============
<%@ include file="/common/taglibs.jsp" %>

<head>
<s:head theme="ajax"/>
</head>

<h3>page: <%= new java.util.Date() %></h3>	
	
<s:include value="/WEB-INF/components/ajaxDiv.jsp" />

<br/>
<div class="separator"></div>
<br/>

<script>
function handleSelect(select) {
	document.multiSelectForm.firstSelectValue.value =
document.multiSelectForm.firstSelect.value;
	if (select.id == 'firstSelect'){
		document.multiSelectForm.secondSelectValue.value = null;
		document.multiSelectForm.thirdSelectValue.value = null;
		try{
			document.multiSelectForm.secondSelect.value = null;
			document.multiSelectForm.thirdSelect.value = null;
		}
		catch(err){}
	} 
	else{
		document.multiSelectForm.secondSelectValue.value =
document.multiSelectForm.secondSelect.value;
	}
	dojo.event.topic.publish('handleSelectTopic');
}

function handleSubmit() {
	try{
		document.multiSelectForm.firstSelectValue.value =
document.multiSelectForm.firstSelect.value;
		document.multiSelectForm.secondSelectValue.value =
document.multiSelectForm.secondSelect.value;
		document.multiSelectForm.thirdSelectValue.value =
document.multiSelectForm.thirdSelect.value;
	}
	catch(err){ 
		// second or third list not present 
	}
}
</script>

<s:form id="multiSelectForm" name="multiSelectForm" theme="simple"
onsubmit="handleSubmit();">
	<s:hidden name="firstSelectValue" />
	<s:hidden name="secondSelectValue" />
	<s:hidden name="thirdSelectValue" />
	<s:url id="handleSelectUrl" action="handleSelectActionAjax" /> 
	<s:div id="multiSelectDiv" href="%{handleSelectUrl}" theme="ajax"
		listenTopics="handleSelectTopic" formId="multiSelectForm"
showLoadingText="false">
	</s:div>
	<br/>
	<s:submit value="Submit" action="submitSelectAction"/>
	<br/>
	submited selection : ${message != null ? message : ''}
</s:form>


-- 
View this message in context: http://www.nabble.com/dependant-%3Cs%3Aselect%3E-updated-with-ajax-%3A-how--tp15616529p15708830.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: dependant updated with ajax : how?

Posted by Musachy Barroso <mu...@gmail.com>.
See the section "linking 2 autocompleters" here:

http://struts.apache.org/2.x/docs/ajax-and-javascript-recipes.html

Same example is in the  Showcase application.

musachy

On Fri, Feb 22, 2008 at 11:56 AM, Dave Newton <ne...@yahoo.com> wrote:
> --- "Stanley, Eric" <Er...@qwest.com> wrote:
>  > Anyone? I'd also like to see this example...
>
>  Using Dojo?
>
>  Are there really no examples of that on the Dojo site?
>
>  Dave
>
>
>
>  > -----Original Message-----
>  > From: thogau [mailto:thogau@thogau.net]
>  > Sent: Thursday, February 21, 2008 10:39 AM
>  > To: user@struts.apache.org
>  > Subject: dependant <s:select> updated with ajax : how?
>  >
>  >
>  > Hi,
>  >
>  > I know this has been asked several times on this list but I couldn't
>  > find what I need...
>  >
>  > I am trying to do something that seems very basic to me, at least it was
>  > with struts 1.x and DWR.
>  > Since struts 2 is shipped with dojo, I would like to use this framework
>  > only (no DWR, nor JWP, ...). doubleselect is not an option for me since
>  > I actually need 3 dependant select tags.
>  >
>  > How can I have 2 select tags where the content of the second one depends
>  > on the selected option in the first one. Of course I would like to do it
>  > ajax flavoured (no page reload).
>  >
>  > Has anybody an example of this running smoothly and willing to share?
>  > Any pointer would be warmly welcome cause I didn't expect to get into
>  > troubles implementing such a basic ajax behaviour...
>  >
>  > Thanks.
>  >
>  >
>  > --
>  > View this message in context:
>  > http://www.nabble.com/dependant-%3Cs%3Aselect%3E-updated-with-ajax-%3A-h
>  > ow--tp15616529p15616529.html
>  > Sent from the Struts - User mailing list archive at Nabble.com.
>  >
>  >
>  > ---------------------------------------------------------------------
>  > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>  > For additional commands, e-mail: user-help@struts.apache.org
>  >
>  >
>  > This communication is the property of Qwest and may contain confidential or
>  > privileged information. Unauthorized use of this communication is strictly
>  > prohibited and may be unlawful.  If you have received this communication
>  > in error, please immediately notify the sender by reply e-mail and destroy
>  > all copies of the communication and any attachments.
>  >
>  > ---------------------------------------------------------------------
>  > 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
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

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


RE: dependant updated with ajax : how?

Posted by Dave Newton <ne...@yahoo.com>.
--- "Stanley, Eric" <Er...@qwest.com> wrote:
> Anyone? I'd also like to see this example...

Using Dojo?

Are there really no examples of that on the Dojo site?

Dave

> -----Original Message-----
> From: thogau [mailto:thogau@thogau.net] 
> Sent: Thursday, February 21, 2008 10:39 AM
> To: user@struts.apache.org
> Subject: dependant <s:select> updated with ajax : how?
> 
> 
> Hi,
> 
> I know this has been asked several times on this list but I couldn't
> find what I need...
> 
> I am trying to do something that seems very basic to me, at least it was
> with struts 1.x and DWR. 
> Since struts 2 is shipped with dojo, I would like to use this framework
> only (no DWR, nor JWP, ...). doubleselect is not an option for me since
> I actually need 3 dependant select tags.
> 
> How can I have 2 select tags where the content of the second one depends
> on the selected option in the first one. Of course I would like to do it
> ajax flavoured (no page reload).
> 
> Has anybody an example of this running smoothly and willing to share?
> Any pointer would be warmly welcome cause I didn't expect to get into
> troubles implementing such a basic ajax behaviour...
> 
> Thanks.
> 
> 
> --
> View this message in context:
> http://www.nabble.com/dependant-%3Cs%3Aselect%3E-updated-with-ajax-%3A-h
> ow--tp15616529p15616529.html
> Sent from the Struts - User mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> This communication is the property of Qwest and may contain confidential or
> privileged information. Unauthorized use of this communication is strictly 
> prohibited and may be unlawful.  If you have received this communication 
> in error, please immediately notify the sender by reply e-mail and destroy 
> all copies of the communication and any attachments.
> 
> ---------------------------------------------------------------------
> 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: dependant updated with ajax : how?

Posted by "Stanley, Eric" <Er...@qwest.com>.
Anyone? I'd also like to see this example...

-Ryan 

-----Original Message-----
From: thogau [mailto:thogau@thogau.net] 
Sent: Thursday, February 21, 2008 10:39 AM
To: user@struts.apache.org
Subject: dependant <s:select> updated with ajax : how?


Hi,

I know this has been asked several times on this list but I couldn't
find what I need...

I am trying to do something that seems very basic to me, at least it was
with struts 1.x and DWR. 
Since struts 2 is shipped with dojo, I would like to use this framework
only (no DWR, nor JWP, ...). doubleselect is not an option for me since
I actually need 3 dependant select tags.

How can I have 2 select tags where the content of the second one depends
on the selected option in the first one. Of course I would like to do it
ajax flavoured (no page reload).

Has anybody an example of this running smoothly and willing to share?
Any pointer would be warmly welcome cause I didn't expect to get into
troubles implementing such a basic ajax behaviour...

Thanks.


--
View this message in context:
http://www.nabble.com/dependant-%3Cs%3Aselect%3E-updated-with-ajax-%3A-h
ow--tp15616529p15616529.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly 
prohibited and may be unlawful.  If you have received this communication 
in error, please immediately notify the sender by reply e-mail and destroy 
all copies of the communication and any attachments.

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