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.
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>
Hello StackOverflow community,
I'm stuck with Struts 1.3.10 (no, I can't try migration to a better framework since my project was given to my team with 70% completion). I'd like to add HTML tags to the Struts taglib.
I already know that for some tags (like placeholder) you can use jQuery but I'm not interested in that. I want to modify Struts sources. I've been looking for on the internet but nothing showed up that can help me. Maybe you can give me a hand with these matter.
Examples of what I want to do:
<html:text property="someProperty" placeholder="Hi..." length="30">
or
<html:checkbox property="choice" data-on-color="primary" data-off-color="info">
or add any HTML property I want.
Thanks in advance, I hope you can guide me.
You do not need to rewrite Struts 1.3.10, only you need to write the desired HTML5 form. You can convert the Struts tag:
<html:text property="someProperty" placeholder="Hi..." length="30">
to HTML5 (using some tags for the value):
<input type="text"
name="someProperty"
value="<bean:write name="nameOfForm" property="someProperty" />"
placeholder="Hi..."
maxlength="30"
size="15">
Note: nameOfForm must the name of the form defined in the file struts-config.xml.
or (using EL):
<input type="text"
name="someProperty"
value="${nameOfForm.someProperty}"
placeholder="Hi..."
maxlength="30"
size="15">
Check the generated HTML with Struts tags, and performs the conversion to HTML5.
(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 use
<form action="someClass">
<s:textarea name="name" label="Name"/>
<s:checkboxlist list="{'Male','Female'}" name="gender" label="Gender"/>
</form>
instead of
<s:form action="someClass">
<s:textarea name="name" label="Name"/>
<s:checkboxlist list="{'Male','Female'}" name="gender" label="Gender"/>
</s:form>
Because <s:form> tag's default theme "xhtml" is not ok with my CSS and theme "simple" can't show validation errors by itself without the use of fielderror tag.
So is it valid to use it?
I'm ok by using like that till now. Is there any error that I will face for using like that in the future?
Of course you can use plain HTML tags.
Please remember that all JSP custom tags do, in the end, is render HTML.[1]
As Roman states, you'll lose things like the automatic filling of values, the retrieval of labels via the attribute name or key, the display of error messages and styling, and so on.
Your best course of action may be to create your own theme and use your own templates and CSS, or use the "css_xhtml" theme, and supply your own styles. Which is better to do depends on a fair amount of information we don't have, but the CSS HTML version is fairly flexible other than having to define the <br/> tag as being inline because it's currently used in the theme where it shouldn't be.
[1] Yes, it could do other stuff on the server side before sending over HTML, like the SQL custom tags. In general, though, the purpose of custom tags is to emit HTML.
You can use <form> tag to do with forms but it's not <s:form tag and you have to maintain it's attributes manually. <s:form renders <form> tag with the nice preset of attributes that exactly communicate with the framework. Omitting it lacks some features supplied by the framework that leads to incorrect usages and bugs.
I currently have a JSP page with a Form for the user to enter their name, but what I want is to get the user forwarded to a different JSP page after form submission and to carry on their name to be used.
I don't want to use JSTL EL just simple JSP uses.
I was thinking of using a bean storing the detail in a session but how would it work.
Thanks.
You'd have JSP enter the info into a form and POST it to a servlet. The servlet would validate the form input, instantiate the bean, add it to session, and redirect the response to the second JSP to display.
You need a servlet in-between. JSPs using JSTL are for display; using the servlet this way is called MVC 2. Another way to think of it is the front controller pattern, where a single servlet handles all mapped requests and simply routes them to controllers/handlers.
duffymo you have the best idea but here is a quick solution by just passing things through the JSP.
Here is the JSP with the form
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Simple jsp page</title></head>
<body><form name="test" action="./stackTest2.jsp" method="POST">
Text Field<input type="text" name="textField">
<input type="submit">
</form> </body>
</html>
and then the second page looks like this:
<html>
<head><title>Simple jsp page</title></head>
<body><%=request.getParameter("textField")%></body>
</html>
And then put information in a hidden field, you can get information by using the request.getParameter method. This just prints out what was in the form, but using the same idea for inputting it in to the hidden field in a form. I recommend this as all my experience with sessions have ended in a failure. I STRONGLY DO NOT Recommend this method, MVC is a much better way of developing things.
Dean