I am creating a project in struts2
I have created a Registration.jsp page like below.
<s:form name="registration" action="Registration" >
<s:textfield name="user.userName" label="UserName"></s:textfield>
<s:textfield name="user.userName" label="Password"></s:textfield>
<s:textfield name="user.userName" label="Re-Enter Password"></s:textfield>
<s:textfield name="user.userName" label="Name"></s:textfield>
<s:textfield name="user.userName" label="DOB"/>
<s:textfield name="user.userName" label="email"></s:textfield>
<s:textfield name="user.userName" label="Portfolio Name"></s:textfield>
<s:submit></s:submit>
</s:form>
As seen above I am trying to give the all fields same name and it is used in setting the values..
After calling the action when I am trying to redirect the result to a jsp and trying to display the userName it will display like below
if we input a, b, c, d and e as the parameter respectively in the given fields. I am getting the output exactly(, included in output) like that - Hello a,b,c,d,e..
I am not getting why is this happening.. does anyone has an idea?
This is how client to server communication works.From HTML perspective everything will be sent over to server using key-value pairs of String.
The value being set in the Action class and interpreted as Collection/Array is a feature S2 and its conversion mechanism (OGNL and XWork conversion ).
So when your values being sent as a key with values like user.userName=a,b,c,d,e and you have declared a collection/Array in your action class so S2 type conversion mechanism is coming in to action and converting these values to Array/Collection and setting them in the respected property in your action class.
In the reverse case HTML will know only String and again from the server they are being sent as key and values and since you are not asking your S2 mechanism to come in to play so this is getting printed in your HTML as per your description.
When you submit this page, the url will look like ?user.userName=a&user.userName=b&user.userName=c&user.userName=d&user.userName=e. Struts2 considers this as a Collection or Array. When you take this value to String typed parameter, the values are comma separated and stored. Later on, this is what getting displayed in your page after the action call.
Related
I want to pass the value of a variable that I retrieve from one servlets to another servlets. I don't want to use session. For example when I submit the form I want the value of nickname to pass to the second servlet.
This value Nickname
<li input type="hidden" name="nickname" value="${fn:escapeXml(obj.nickname)}">
Nickname: <c:out value="${obj.nickname}"/>
</li>
I use this link Stack Example
in your form you can set a hidden input which you pass to your second servlet:
<input name="nickname" value="${obj.nickname}" type="hidden"/>
This code is wrong, you can't have the word "input" randomly as a list attribute.. You also cannot have the type, name or value attribute on a list tag..
<li input type="hidden" name="nickname" value="${fn:escapeXml(obj.nickname)}">
Nickname: <c:out value="${obj.nickname}"/>
</li>
Check out the link here below, it shows the legal html tags you can use to send information with a form. You can only use these. So a list won't work.
https://www.w3schools.com/html/html_form_elements.asp
I have a parent jsp file that includes two child jsp's. I have a variable defined in the parent file like so:
<c:set var="test" value="N" />
which I then pass into two child jsp files:
<div id="div_data_1" style="display:none;">
<jsp:include page="page1.jsp">
<jsp:param name="controlFlag" value="${test}"/>
</jsp:include>
</div>
<div id="div_data_2" style="display:none;">
<jsp:include page="page2.jsp">
<jsp:param name="controlFlag" value="${test}"/>
</jsp:include>
</div>
In my page1.jsp file I then store the value in a hidden div:
<div id="cashAuditFlag" style="display: none;">${param.cashAuditFlag}</div>
I then have a button in page1.jsp that, when clicked, I want it to change the value of the parent variable ${test} to "Y". This in turn would then change the value of ${test} in page2.jsp which would cause a change in page2.jsp.
I basically want to have a child jsp communicate an update to another child jsp, both of which belong to the same parent.
A - Is this the best way of doing this process?
B - How can I have a child jsp update the parent jsp variable?
Thank you!
To understand this, you need to understand scopes. Think of a scope as a bucket that variables go into when they are defined. Some code only has access to some of those buckets.
Request scoped variables are available in any part of the code that knows about the http request. There is only one request scope per HTTP request to your webapp. In your example, the parent file, page1.jsp, and page2.jsp all have access to that request scope. For example, if you did this in your parent JSP page:
<c:set var="test" value="N" scope="request" />
...it would put the "test" variable into the request scope bucket with a value of N.
Then, if you want to view or modify this value in either page1.jsp or page2.jsp, you don't even need to have a jsp:param element in your jsp:include, so you can just do this:
<div id="div_data_1" style="display:none;">
<jsp:include page="page1.jsp"/>
</div>
<div id="div_data_2" style="display:none;">
<jsp:include page="page2.jsp"/>
</div>
So, if you want to display this in either child page, you can simply use Expression Language and tell it to look in the requestScope for the variable named "test" by using the requestScope object:
<p>The Test Variable is: ${requestScope.test}</p>
Similarly, if you wanted to modify this variable in either childPage, you can simply do another c:set statement:
<c:set var="test" value="Y" scope="request" />
Now, if you print out the value of ${requestScope.test} in any page, it will be Y.
In your example, when you used the c:set statement without scope="request", you created a variable in that jsp page's "page scope", meaning you could only access that variable in the jsp code you wrote in your parent jsp page.
Now, as to whether this is the best way to do this...
You say you have a button in page1.jsp that, when clicked, should change the test variable to Y and cause some display change in page2.jsp. Here's the flow of what would need to happen:
User vists your JSP page at some url, like "mywebapp/testPage.jsp"
The page renders. The initial c:set statement runs which sets the "test" var to a value of "N".
The user clicks the button, which causes the browser to send a new request but adds a request parameter, resulting in a request of something like "mywebapp/testPage.jsp?buttonClicked=1"
All your JSP pages render again (keep in mind JSP does not do things "dynamically" i.e. without a browser refresh - when you click a button, the browser sends a new HTTP request to the webserver and your JSP is rendered again).
At the top of your parent JSP, you need logic that checks whether the buttonClicked request parameter is present. If so, it sets the value of test to "Y" instead of "N".
So, to explain: in order to have the page render differently based on whether the button was just clicked, you would need to have your button pass a request parameter when it is clicked, and you would need to have your JSP look at the new request to find that request parameter (to see the value that was submitted when the button was clicked). If you don't have your code check that, then, every time your page loads, your parent JSP page will just keep re-setting the test variable to N because your initial c:set value="N" statement will always run when the JSP renders.
So, if you want something like the case you described, you'd have to do something like this in your parent JSP page:
<c:set var="test" value="N" scope="request"/>
<!-- Here's the check for whether the request parameter is present -->
<c:if test="${not empty param.buttonClicked}">
<c:set var="test" value="Y" scope="request"/>
</c:if>
<div id="div_data_1" style="display:none;">
<jsp:include page="page1.jsp"/>
</div>
<div id="div_data_2" style="display:none;">
<jsp:include page="page2.jsp"/>
</div>
...then in page1.jsp, where you have the button, you would do something like this:
<input type="submit" name="buttonClicked" value="1" />
That way, when the button is clicked, it will refresh the page and add a new request parameter called buttonClicked with a value of "1". In the parent JSP, it will see that this request parameter is present (with "not empty param.buttonClicked") and it will set the value of the test variable to "Y", overwriting the previous assignment of "N".
Then in your page2.jsp, you can access the value of test using ${requestScope.test} at any point on your page.
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
How to get a value that is sent by a form in jsp.
This is because you are not sending Name back to the controller.for sending values back to the action all you need to send them as form fields where as in your case you are just displaying them but not sending them back.
One solution is to store the name as a hidden field like
<s:form action="AddProduct" >
<tr><td>
<label for="name">Name:${Product.name}</label>
<s:hidden name="name" value="%{Product.name}"/>
</td></tr>
<s:submit/>
</s:form>
Make sure you should have name property in your action class or if you have a bean in your action class who has name property than the name of hidden field shd be beanName.name
My web server - tomcat sets two attributes in the session. In the jsp page, am retrieving them as
<% String age = (String) session.getAttribute("age"); %>
Am setting this value to a hidden field in the form in the same jsp file.
<input type="hidden" name="age" id="age" value="<%=age%>" />
And am trying to use this value in javascript file as below
document.cForm.age.value
But this value is null for most of the times. Sometimes, the variable to set to proper value? Is there any reason for this inconsistency? Please explain
Try to get the value on DOM load not inline. i mean inside a $(document).ready(function() {})