I have the followin question, how will I pass multiple values from one jsp page to another? I have i piece of code here, which works fine, but it only sends one value from one page to another (year):
<form method="post" action="Display data.jsp" name="inputpage" >
<select name="year">
<option value="2010">2010</option>
<option value="2011">2011</option>
</select>
For example if I had another value, for example
String str = "value";
Is it possible to send it using form post method as well? I googled it, and the answer I found included loops and too much code, is there short and simple way of doing it? Many thanks!
When you submit the form all values of the form will be passed, they only need to be inside the form. You can read other values normally by using:
request.getParameter(ParamName)
Take a look at this article for more information
You can send as many variable you want by Form Method.
For sending the value of String Str, assign its value to hidden field as:
<input type="hidden" id="hidden1" value=<c:out value="${variableName}" />
where variableName=str.
Could you use a hidden input inside your form to pass other data using the form post?
<input type='hidden' id='myExtraData' value='hello' />
Related
I'm having a bit of an issue with getting the value of a select in java.
This is the code inside of my java controller:
String topicName = request.getParameter("select_topic");
This is the code inside of my JSP file:
<select class="topic-form" name="select_topic" style="height:40px; " id="select_topic">
<option>Select topic</option>
</select>
I'm generating options for the select dynamically and then trying to get the selected option inside of java. The problem is I tried to see what the code in java returns and it's a "null" value. I've also made the jsp return the values inside the console to check if the values are right and they are. So all I can think of is that the value somehow doesn't get taken by topicName. I have also checked to see if the function gets called and it does.
Lets say your JSP page with the form is called "topic.jsp" :
Make sure that your <form> tag has an action param (ex. action="topic.jsp")
Make sure that each of the <option> tags have value param. (ex <option value="-1">Select</option>
Example:
<form action="topic.jsp" method="get">
<select class="topic-form" name="select_topic" id="select_topic">
<option value="-1">Select topic</option>
<option value="1">Topic 1</option>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
Debugging :
It is helpful to set form method to GET so the request parameters will be included in the address bar.
HttpRequest object has a couple useful methods:
request.getQueryString(); // if (request.getQueryString() != null) System.out.println("QS: " + request.getQueryString());
request.getParameterMap() // Map<String,String[]> (Key = form object name, Value[] = respective values)
I want to pass values of two variables when a link is clicked to another page I am using query parameter but I am only able to send one variable through it. I know about session.setAttribute() but don't know how can I use it based upon links...Foreg:
<p> < </p>
<a href="Search.jsp?item=<%=search%><%session.setAttribute("val",value);%>" class="classname1" > > </a>
This is my code I know its wrong..I just want is If I click on first link than value1 should be passed and If I click on 2nd link value should be passed.P.S.:I have already passes search variable through query parameter but now If I try to pass second parameter through session only the final value i.e second initialized value only counts? what to do?
EDIT:
suppose my code is this:
<form class="navbar-form navbar-right" action="Search.jsp" method="get">
<input type="text" class="form-control" placeholder="Search..." name="search">
Here one variable search is passes through form How can I pass another variable value?should it be like:
<form class="navbar-form navbar-right" action="Search.jsp?item1=<%=value%>" method="get">
<input type="text" class="form-control" placeholder="Search..." name="search">
You can send multiple parameters like,
href="Search.jsp?item=<%=search%>&item2=value2&item3=value3.."
Also to add <%session.setAttribute("val",value1);%> will be executed at server side irrespective of the click of the hyperlink.
For the form you can add another input parameter in the form,
<input type="text" name="item1" value="<%=value%>">
You need to add some separator in between two values e.g.#
And while reading at server side you can split those values based on that separator
You may try using this example to send multiple values:
With same name
With diff name
Separate the two values by using &:
<a href="Search.jsp?item=<%=search%>&item2=<%=value2%>">
On your search.jsp fetch values as :
request.getParameter("item");
request.getParameter("item2");
You need to call session.getAttribute in the place where you are calling session.setAttribute and session.setAttribute should be called in controller or in the jsp before the link tag to set the value. Please separate values like Search.jsp?item1=value1&item2=value2
I'm stuck in a situation where I've an input element in a JSP where user enters tags. E.g. java, foo, bar, anotherTag..etc
<c:url var="saveUrl" value="/create" />
<form:form modelAttribute="myAttribute" method="POST"
action="${saveUrl}">
<form:input path="myTitle" />
<form:textarea path="myPost" />
<form:input type="text" id="tagInput"path="???" />
<input type="submit" value="create" />
</form:form>
Now in my domain model corresponding to this input is a
private List<Tag> listOfTags
How to bind a csv to a List. If I enter listOfTags in the path(which is wrong for obvious reasons), I get incorrect binding exception.
How do I convert(or bind) a csv to a List so that the Spring form is submitted properly and the listOfTags get the tags entered in the JSP.
What is the best way to achieve it?
Please help.
I'm not sure but try this. Do a simple html input :
<input type="text" id="tagInput" name="myTags" />
And then in your controller do something like :
#RequestMapping(value="/create", method=RequestMethod.POST)
public void create(..., #ModelAttribute("myAttribute") MyClass myAttribute,
#RequestParam("myTags") String myTags, ...) {
...
myAttribute.setListOfTags(Arrays.asList(myTags.split(",")));
...
}
Note : for more generic ways to bind and convert objects, you may want to take a look at PropertyEditors and Converters.
I suggest try to bind it directly to listOfTags property. And to make it work just add contructor with one argument of String type (or define static method valueOf(String)) to Tag class.
Pretty sure you could do something like this:
<c:forEach var="i" begin="1" end="10">
<form:input type="text" path="listOfTags" />
</c:forEach>
Where you get the user to enter each tag into a separate text input. This is because Spring will automagically bind multiple inputs with the same form name to a List, when it does its binding.
You could use some jQuery sugar to only show one or two and then provide a widget to show more tag inputs. Or even write some cool JS to populate the inputs from a single text input just like StackOverflow does when you add tags.
I am looking for some suggestions and tips regarding Servlet/JSP issue I am trying to solve. I need to access a Servlet variable in JSP page which I am passing through the request.setAttribute, then the variable needs to be passed onto another Servlet through doPost. I am able pass that to the page and can display/print, but I would like it to be not displayed, but just passed on to the Submit button.
Here's my Servlet code:
request.setAttribute("jsession", jsession);
I can do the following and it works, but it's displaying it on the page and the end-user doesn't need to see this:
<select name="jsession">
<c:forEach var="jsession" items="${jsession}">
<option value="${jsession}">${jsession}</option>
</c:forEach>
</select>
But, I am looking to do something like this:
String sess = ${jsession}.
This will be then passed onto the Submit button, maybe I am over-thinking this. Can some veterans point me in the right direction. I appreciate all the effort the veterans take in here. Thank you so much.
If I understand correctly, you just need a hidden field instead of your select box:
<input type="hidden" name="jsession" value="<c:out value="${jsession}"/>"/>
I am trying to make a dynamic form using Spring forms. Basically, the form gets a title of learning activity and then there's the a button below it that says, "Add one more Learning Activity". This makes it possible for the user to add one more learning activity. I want him to be able to add as much as he likes.
I haven't tried this before so obviously I encountered errors with the first solution I thought of. I really had a feeling doing what I did will generate an error but just do drive home what I am trying to do, here's the code:
<script language="javascript">
fields = 0;
function addInput() {
document.getElementById('text').innerHTML += "<form:input path='activity[fields++].activity'/><br />";
}
<div id="text">
<form:form commandName="course">
Learning Activity 1
<form:input path="activity[0].activity"/>
<input type="button" value="add activity" onclick="addInput()"/>
<br/><br/>
<input type="submit"/>
</form:form>
<br/><br/>
</div>
You can't use <form:input> within the javascript because is a jsp tag that runs on the server-side.
However, there's nothing magical about how an HTML input gets bound to a field in the Spring command object; it's just based on the name. So in your javascript, add a new
<input type="text" name="activity[1].activity">
(for example -- obviously you'll increment the index).
Another option I've used for more complicated controls is to grab the HTML of the existing control (which was created using the Spring form:input tag) and clone it, replacing the indexes with the incremented number. This gets a lot easier if you use jQuery.
EDITED TO ADD:
One issue that may cause you problems: you're appending your new input box to the end of your outer div ("text"), which means it's not inside the form tags. That won't work.
Is <form:input> a JSP tag? If so, then client-side Javascript to add <form:input> nodes to the DOM will have no effect - since the server-side JSP engine is not interpreting those.
Your Javascript needs to work with raw HTML and DOM, such as adding an <input> element.