I would like to have an input 'submit' calling a method from the Java EE java servlet that is responsible for showing the JSP.
<form action="????" method="post">
<input type="image" src="img/icons/cross.png" alt="Remove widget">
</form>
The above form method (another solution is also welcome) should alert the Servlet and include the id of the widget that has to be removed. How can I accomplish something like this, without using scriptlets and with using a MVC set-up.
The servlet included the widgets in the JSP by using the following line of code:
request.setAttribute("widgets", widgets);
Thanks:)
Thanks guys. With the suggestion of Alexandre I managed to fix my problem:)
Related
I'm fairly new to Thymleaf and Spring, and I'm taking a course that is using JSP for client view pages. I couldn't set up JSP using Spring Initializr so I resorted to using Thymeleaf instead (I figured I would end up using Thymeleaf normally anyways)
My question is, how Do i pass form data from one HTML, to another page? I've tried looking at the documentation, and googling around and couldn't find anything.
I have already tried using, a class to set up the Objects, and variables (and i know this is a way of doing it, i'll eventually get to the part of learning how to do that), but I'm just trying to get data from one form to another page.
In JSP you can do this
htmlpageOne.html
<form action="processForm" method="GET">
<input type="text" name="studentID"/>
<input type="submit"/>
</form>
htmlpageTwo.html
<body>
Student ID: ${param.studentID}
</body>
Using JSP you can call the studentID from the past form without having to store it in any Object and having to create any thing else.
I know the data won't really go anywhere and isn't stored, but just for simplicity and demonstration, is there any way to do the same with Thymeleaf?
any help or direction would be very much appreciated
Yes, thymeleaf supports request parameters.
https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html#request-parameters
<body>
Student ID: <span th:text="${param.studentID}" />
</body>
(Oups.. Sorry for my english :) )
In my web application, Struts2 is used as the main Servlet dispatcher and filter. But For some reasons, i have a custom filter and a custom servlet used for a specific url "/book".
But I have some commons jsp... i had some issues when the custom Servlet should display my request attributes in the JSP because of the struts tags (implemented before). So i changed these tag by the jstl taglibs and it works now.
But... In one JSP, the main (lol)... I have a search form.. This JSP is included in several JSPs and could be called by Struts and the custom Servlet..
With only Struts the tag was "< s:form>.." and when the form was submitted, all sended values was kept in the input... But now, because of the custom Servlet i use a simple html form which is calling the struts action "search.do".
As source code is below:
<form method="post" action="<c:out value="${contextPath}"/>/search.do" name="search" id="search">
<input type="text" id="search_searchWord" value="" maxlength="200" size="100" name="searchWord">
<div align="right">
<input type="submit" value="Ok" name="searchButton" id="search">
</div>
<select id="search_searchCrit" name="searchCrit">
<option value="0">Crit1</option>
<option value="1">Crit2</option>
<option value="2">Crit3</option>
</select>
</form>
My problem is the search word and the selected option are refreshed after the submit. I need to keep them !
Is there a way to use the struts taglibs with a Standard Servlet ?
Or Do you have another solution to keep the submitted information ?
Thanks all !
take each field value from the input field and write js function to fill each field in jsp source code of your page.
function selectedValue(){
var value =<%=request.getParameter("searchCrit")%>;
if(value !=null)
{
document.getElementById('search_searchCrit').innerHTML=value;
}
}
I found a solution with the help of #java_seeker.
In my Struts action, i got the request through this way :
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("searchWord", this.getSearchWord());
There is two different way to do this, see: http://www.mkyong.com/struts2/how-to-get-the-httpservletrequest-in-struts-2/
The attribute is setted in each method (in the action) that could refresh the page.
Then, i just recovered and set the attribute from the request as a variable with a jstl tag and display it as the value of my html input:
<c:set var="searchWord" value='<%=request.getAttribute("searchWord") %>' />
<input type="text" id="search_searchWor" value='<c:out value="${searchWord}" />' name="searchWord">
For the , i just used an <c:choose><c:when test=""></c:when><c:otherwise><c:otherwise></c:choose> to set the selected choice.
Now all value are always displayed. Maybe it's not the very good way to display share the same JSP between a standard servlet and a Struts action, but it works. I'm open to try a better solution if you have one! Thanks all!
I want to ask you how to populate form in jsp (form:form or typically html form) with existed java object's attributes. And after changing them, how can I save this property? I googled for long but I cannot find anything about that. Thanks for advance for help.
That depends on the frameworks you use.
If you use any component based MVC frameworks, the binding between your form, your object can be done. So that your form will be pre-populated with the values available in your object and vice-versa. Example frameworks are JSF, Spring MVC
If you want to do it manually
while generating form in your JSP, you can manually iterate through the java object in JSP and assign value to the form fields using scriptlets
For example
<input type="text" name="name" value='<%=yourObject.getField()%>' />
if you are using a regular form you can use the following notation.
<html>
<body>
<form action="somepage.jsp">
<input type="text" name="name" value='<%=request.getParameter("name")==null?"":request.getParameter("name")'/>
<input type="submit"/>
</form>
</body>
</html>
you can access the parameters on the server side by using the same request.getParameter("name") call..
I would strongly recommend you to use a framework here like Spring MVC, Struts2 etc.,
Frameworks such as struts1,struts2 and more can do the things for you. Basically you need some kind of beans or DTO's for holding jsp-page data.
In struts2 ,its is very simple,dynamic and powerful.Struts2 holds the jsp-page data in its value-stack.
But in jsp using servlet you have to use java-objects on jsp-page inside scriptlet-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 have got two forms in a page. The second form has got a file upload input component along with other controls. My requirement is to submit the second form with the file and refresh only that form without refreshing the other form. If it was only normal components, I could have done this easily in Ajax. But when the form is having a file component, I feel its not that straigh forward. Please suggest any ideas to do it???
You can still use AJAX on a form with file components. Maybe you can use the jQuery library (if you are not already) since that makes these tasks trivially easy.
Put the second form in an iframe.
The way I have done it in the past is to hide an iframe on the page. Then set the target of the file upload form to the name that was given to the iframe. If you need to be xhtml compliant you can use JavaScript to create the iframe after the page loads and to set the target on the form. The code will look something like this. You can apply css to the frame to hide it.
<iframe name="myFrame" src="blank.htm"></iframe>
<form action="uploadFile.php" method="post" enctype="multipart/form-data" target="myFrame">
<input type="file" name="myFile"/>
<input type="submit" value="Upload"/>
</form>