wood burning stoves 2.0*
The moose likes JSP and the fly likes get value of <select> and read data from list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "get value of <select> and read data from list" Watch "get value of <select> and read data from list" New topic
Author

get value of <select> and read data from list

James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35

I need to set values of input based on value in <select> from ${requests}. User select item - in input values from list appear.

the java code for value is sth like this


How to do it?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

James Whillis wrote:I need to set values of input based on value in <select> from ${requests}. User select item - in input values from list appear.

I'm not getting what you are trying to do. Set what values, when?

the java code for value is sth like this

sth?

I'm not understanding the Java code. It looks like a mix of Java and EL?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Bear Bibeault wrote:
James Whillis wrote:I need to set values of input based on value in <select> from ${requests}. User select item - in input values from list appear.

I'm not getting what you are trying to do. Set what values, when?

the java code for value is sth like this

sth?

I'm not understanding the Java code. It looks like a mix of Java and EL?


Well there is <select> tag. It contains values from list ${request}. Now when user chooses some option - in <input> appears values from list ${request} where parameter id - selected value.
For example


I don't know how to explain. (My English knowledge is not enough)

Every time when use selected new value - values in inputs change too.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Ah... in that case you'll need to use JavaScript. JSP is of no help.
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Bear Bibeault wrote:Ah... in that case you'll need to use JavaScript. JSP is of no help.

Oh...I see. So I need sth like this?


But how to get value of the field of ${requests} list? The same way as on jsp page? I mean

like this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

James Whillis wrote:
So I need sth like this?

sth?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Remember that you can use JSP to "write" anything you want into the page. So if you need a JavaScript array that contains the list, how would you do that?
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Bear Bibeault wrote:
James Whillis wrote:
So I need sth like this?

sth?


Something(maybe smth?)

Bear Bibeault wrote:Remember that you can use JSP to "write" anything you want into the page. So if you need a JavaScript array that contains the list, how would you do that?

Do what?

?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

James Whillis wrote:
maybe smth?)


No, something is spelled "something".

James Whillis wrote:
Do what?

?


OK, start off with what you want to end up with. In other words, how would you write the JavaScript to create a list of those values? Then, figure out how to write a JSP construct that will create that.
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Bear Bibeault wrote:
OK, start off with what you want to end up with. In other words, how would you write the JavaScript to create a list of those values? Then, figure out how to write a JSP construct that will create that.


Here the function


For this form


I am going to test but this is my idea.
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
After some time I've found such solution


But I can't get value from <select> with select id="re" name="requestid". Alert always shows null


It seems I have to use ajax
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

You are using the JSTL and EL elsewhere; introducing scriptlets now is madness.

Stop just stabbing in the dark and follow my suggestion: what JavaScript construct do you need to list the values? No JSP for now. Just figure out what the JavaScript should be. Then, and only then, we can figure out the JSP to create it.

How can you create something if you don't know what it's supposed to be?
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Bear Bibeault wrote:You are using the JSTL and EL elsewhere; introducing scriptlets now is madness.

Stop just stabbing in the dark and follow my suggestion: what JavaScript construct do you need to list the values? No JSP for now. Just figure out what the JavaScript should be. Then, and only then, we can figure out the JSP to create it.

How can you create something if you don't know what it's supposed to be?


I've found such solution


But form still not filled with data. And even alerts doesn't work.
Here AjaxAction. In debugger json string created correctly.

Also every action returns string and in ActionServlet I choose where to redirect user

I thought that it doesn't matter where to redirect it - ajax will return user to the page from where he sent request. And it is really does not change user's page. But form is not filled with data. Even if keys in json request has different names from form's objects - alert should work. But it doesn't.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

OK, I wish you luck with your question.
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Bear Bibeault wrote:OK, I wish you luck with your question.


I see...Ok then.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

I've made lots of suggestions that you ignore, and you keep going off and finding code elsewhere. Why waste my time?
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Bear Bibeault wrote:I've made lots of suggestions that you ignore, and you keep going off and finding code elsewhere. Why waste my time?


I didn't ignore them. I simply don't understand what does mean what JavaScript construct do you need to list the values and how can it help. Because in javascript I always can get element by its id and do whatever I want with it.

I can use each also.


But it does not matter now I suppose.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

OK, let's start over and stop just copying code from elsewhere. Let's write it ourselves.

And when you don't understand something, ask! If you just go and try something else, it just looks like you are ignoring the posts.

Here's what I'd like you to do: ignoring JSP, write an HTML file with the HTML and JavaScript that's needed to do what you want. Hard-code any values. Once we have that working, then, and only then, do we figure out how to use JSP to supply the values in a non-hard-coded way.

So start simple and get as close as you can to what you need. Do not copy anyone else's code that you do not understand. Let's write your own code.
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Bear Bibeault wrote:OK, let's start over and stop just copying code from elsewhere. Let's write it ourselves.

And when you don't understand something, ask! If you just go and try something else, it just looks like you are ignoring the posts.

Here's what I'd like you to do: ignoring JSP, write an HTML file with the HTML and JavaScript that's needed to do what you want. Hard-code any values. Once we have that working, then, and only then, do we figure out how to use JSP to supply the values in a non-hard-coded way.

So start simple and get as close as you can to what you need. Do not copy anyone else's code that you do not understand. Let's write your own code.


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Cool. Does it work? Does it do what you want?
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Bear Bibeault wrote:Cool. Does it work? Does it do what you want?


Yes. It should work like this. But of course data sources are another objects.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

OK, now comes the fun part!

Identify all of the hard-coded values that need to be supplied at run-time from server-side data.

Then -- and here's the tricky/interesting part -- stop thinking of the HTML and JavaScript as anything but text. That's actually kinda hard to do at first, but to the JSP engine, everything that's in your static HTML file right now is just template text. As far as the JSP engine is concerned, it could be the text of the Magna Carta. But JSP doesn't know anything at all about HTML, or CSS, or JavaScript. To the JSP engine, it's all just text.

So, once the values that need to be provided at run-time are identified, you need figure out how to get JSP to emit that and make the browser-side text look just like your static text.

Make sense?

Simple example: let's say that you wanted to end up with this line: var xyz = 213; but the value 213 is in a scoped variable that we named whatever. In order to emit that line of text, we'd write:
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Bear Bibeault wrote:OK, now comes the fun part!

Identify all of the hard-coded values that need to be supplied at run-time from server-side data.

Then -- and here's the tricky/interesting part -- stop thinking of the HTML and JavaScript as anything but text. That's actually kinda hard to do at first, but to the JSP engine, everything that's in your static HTML file right now is just template text. As far as the JSP engine is concerned, it could be the text of the Magna Carta. But JSP doesn't know anything at all about HTML, or CSS, or JavaScript. To the JSP engine, it's all just text.

So, once the values that need to be provided at run-time are identified, you need figure out how to get JSP to emit that and make the browser-side text look just like your static text.

Make sense?

Simple example: let's say that you wanted to end up with this line: var xyz = 213; but the value 213 is in a scoped variable that we named whatever. In order to emit that line of text, we'd write:


I know about it. But I've got a list of objects. Not some variable with a single value.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

OK, so let's look at this line:

var array1 = [ 'item 1','item 1','item 1' ];

(By the way, please use better names in real code.)

And let's say that the three values are in a List that's a scoped variable named myList.

Remembering that everything that is not one the values is just meaningless (to the JSP engine) template text, how would you write JSP code to emit that line given the scoped variable that contains the list?
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Bear Bibeault wrote:OK, so let's look at this line:

var array1 = [ 'item 1','item 1','item 1' ];

(By the way, please use better names in real code.)

And let's say that the three values are in a List that's a scoped variable named myList.

Remembering that everything that is not one the values is just meaningless (to the JSP engine) template text, how would you write JSP code to emit that line given the scoped variable that contains the list?


I don't understand.
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Also
gives error.
think that ${requests} is a string and I have no access to it items besides symbols like [ c and etc.


Oh! I get it! I need to use
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
I've solved it.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Not quite how I would do it, but if it works for you, be sure to remember how to apply the lesson.

:jumpingjoy:
James Whillis
Ranch Hand

Joined: Jul 27, 2013
Posts: 35
Bear Bibeault wrote:Not quite how I would do it, but if it works for you, be sure to remember how to apply the lesson.

:jumpingjoy:


The problem is that I don't have any time to lessons now - I need to finish project on this week or better even by tomorrow morning(well it's 2 AM here)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get value of <select> and read data from list