How to pass value or param from jsp to Spring controller? - java

I'm trying access some value from my jsp file in Spring controller. All examples that I've found was with <input> or <form> code, but I want to pass "clear" value (just String), for example <c:set var="name" value="Ololo"/>or smth like this and print it in Conroller.

You can't. Data can flow only in one direction, from controller to view (jsp). If you have to send information back to the controller then you should make http request. That's the reason you only see <form> and <input> in examples

Related

Calling part of jsp page from Servlet or Java Class

I need to call small part of JSP page(like only div load) when a method is invoked? I know we can load whole JSP page from Servlet. Is this possible or any other solution for this?
know we can load whole JSP page from Servlet. Is this possible
Yes it is possible, you forward your request to jsp from servlet and it will render that jsp, here is an exact same example
1) Set some session or request variable equal to some value, say boolean myVar = true.
2) Redirect from your jsp page from your servlet.
3) In the jsp page, check for particular condition for the variable myVar. If condition is fulfilled, using scriptlets or JSTL display only the relevant part of you jsp.
You can use AJAX(Asynchronous Javascript and XML) for this purpose
For example, Suggestions, that appear without any refresh for auto-complete in google search, are using Ajax. JSP code will contain the Ajax code and will send the information to the servlet in form of XmlHttpRequest object and then takes the response from servlet as xmlhttp.responseText. The result can be written at JSP page by using DOM.
To initiate this process, you need to use the onkeyup in input tag like this:
<input type="text" onkeyup="methodName(this.value)"
Learn more about Ajax
To use AJAX with Java, try this
Ajax for Java Web Applications
$('#myDiv').load('serverPage.jsp #server_div_id');
OR
$.ajax({
url: 'serverPage.jsp',
success: function(data) {
data=$(data).find('div#id');
$('#mydiv').html(data);
}
});

How to pass dynamic parameter value in a link using Struts taglib?

I'm trying to use Struts framework in my project. I want to use the html:button to send parameter in the link but I don't understand how to make it.
In other words, I want to translate this line:
<input type="button" onClick="window.location.href='resum.do?action=ViewMessage&&id_message=<%= id_msg %>'" value="View"/>
to Struts taglib, something like this:
<html:button property="" onclick="window.location.href='resum.do?action=ViewMessage&&id_message=<%= id_msg %>'" value="View"></html:button>
But it didn't work.
The html:button tag is used only inside the form tag. See the docs
This tag is only valid when nested inside a form tag body.
Also set the property attribute.

how to check getParameter is set in the project

I am analayising the existing project, in one of the JSP page I saw that
String server=request.getParameter("server");
But I am trying to check How I can find where is this server parameter is set
I searched for setParameter("server"), no luck
can any one suggest on this
Main idea, I need to change the values for the values which is set in "server" parameter.
You can look in any of HTML elements, where they may have tag like <input type='text' id='server' name='server' /> like that. I have given example of text box, it can be anything.
And no, there is no such method called request.setParameter()
You can get this attribute from the html/jsp page from where the form is being submitted.
It is set by the HTTP request that was generated by the client side (such as a browser). For example, an HTML form, upon submission, would result in an HTTP request with parameters for each field. A standalone client (non-browser) can set request parameters by merely adding those parameters to the URL itself.
Therefore, first you have to determine what is generating the HTTP request that results in your JSP page being called. Once you find who's generating the request, it will be very easy for you to find how the parameter itself is set.
In your web.xml first check, from which form present in JSP/HTML the request is coming.
In the corresponding action, you will get all the input fields in the form, which will be fetched in the servelet through request.getParameter('')
This parameter is set when any form is submitted. Check the page which calls this servlet. That page will contain a form having field like <input type='text' name='server' />. If not found then check URL query parameter.
EDIT
In web.xml check which URL is being mapped to your servlet.

Sending text-field value to server without form tag

I need to post a text-field value to server but i have not placed the text-field in side the form tag.Here is the details of my use-case
i have an anchor tag like
LOGIN
This anchor tag is not inside any form tag and i need to send one extra value to the server and don't want that value to append as query string.
I have created a hidden filed and have provided the required value to that hidden field, but when i click on the Login link and its getting to my Controller class this hidden field value is not available.
Is there any way to send that value to server side class as a request parameter?
You can use ajax to do that, i suggest to use Jquery
$.post('loginhandle', {username:$('#username').val(), password: $('#password').val()} function(){});
By using Javascript get value from hidden fields like this
<script>
var name= document.getElementById("login").value
document.getElementById("topage").innerHTML='LOGIN'
</script>
<input type="hidden" name="name" value="ashraf" id='login'>
<div id='topage'>
LOGIN
</div>
You're doing a get rather than a post. You could append to he querystring as this works with get.
Get the hidden field value using javascript before the form is submitted. Use
document.getElementById("hiddenID").value; Append the value obtained in the URL before the form is submitted. The value should be there in the server.
Regards,
Ajai G

How to re-populate form fields on a jsp page after failed server validation

I have a very simple Java MVC web application and am using a servlet to handle form validation. If the form is validated, the request is forwarded to the appropriate view. However, if the form fails validation, the request is forwarded back to the form, which then displays the appropriate error message(s).
My question is this -- what is the most efficient way to re-populate all of the form fields with the data that was originally entered in the form by the user?
I am not using an MVC framework, just simple HttpServlets as the controller with .jsp as the view.
The easiest and probably least effort is to just use
<input name="foo" type="text" value="${param.foo}"/>
This should default to "" when the user first visits the form.
A little more can be done to create a custom tag which binds to the request. However this is probably not the solution you were looking for.
Edit: You may want to use <c:out value="${param.foo}"/> to protect against XSS attack.
Pass the fields back to the jsp as part of the request object. request.setAttribute(..)
Use those attributes to set the form fields.

Categories

Resources