(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!
Related
As we know every jsp program there is a servlet behind the jsp page. I have used a jsp page to make a form (its a very small form), and in the same jsp i used scriptlet tags and made a way to get the inserted form data, and display it using out.print(). but the problem is it when i run it, the form is displayed., but when i submit is, it doesn't recognize the servlet page (error coming as "The requested resource is not available"). i will put the code below., please help me friends to solve this problem. thank you.
i did this in netbeans.
jsp page name is- "hello.jsp"
the servlet page name behind the jsp page is: "hello_jsp.java".
<html>
<head><title>IF...ELSE Example</title></head>
<body>
<form action="hello_jsp" method="post">
<input type="text" name="y"/>
<input type="submit" value="submit"/>
<%
if(request.getParameter("y")!=null) {
String s = request.getParameter("y");
if(s.equals("hello")){
out.print("welcome"+s);
}else{
out.print("not welcome");
}}
%>
</form>
</body>
</html>
My guess is that you need to change
<form action="hello_jsp" method="post">
to
<form action="hello.jsp" method="post">
<!-- ^---- change is here -->
The externally-accesible resource is the jsp, not the servlet. (By default, I'm sure some config-fu could change that.)
Or, of course, if the page is supposed to submit to itself, don't include action at all. The default is to submit to the current page.
<form method="post">
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.
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've got a JSP page which contains a textbox, wrapped in a form. This form's action is set to a servlet.
I would like to manipulate the string (from the user's input in the textbox) before it is sent to the servlet, thus basically carrying out a simple request.setParameter call from the JSP to the servlet. Can this be done? If so how can I obtain the textbox's value in the JSP?
<form action="MyServlet" method="post">
<input type="text" name="txtUsername"/><br/>
<input type="submit" value="Submit"/>
</form>
You cannot do this using JSP code.
Remember, a JSP is processed, outputting its contents to the browser; that's where the JSP's request/response cycle ends.
Your options are:
Using JavaScript.
Using a Filter: http://docs.oracle.com/javaee/5/api/javax/servlet/Filter.html
Call a Javascript function on submit e.g. below:
function fnSubmit(){
document.getElementById("txtUsername").value = "new Value";
document.forms[0].submit();
}
We're converting an older Struts 1.x app from HTML4 to XHTML1.1. To force compliance from the Struts tags, we're adding
<html:xhtml />
to the top of our JSPs.
After doing this, JSPs using
<html:form method="post" action="/foo.do" styleId="bar">
threw the following error:
Cannot specify "styleId" when in XHTML mode as the HTML "id" attribute is already used to store the bean name
I read that The solution is to remove the styleId and use the name of your form bean, as that will be inserted as the id in your HTML. We can take out the styleId, but when 2 forms on the same page submit to the same action, they end up with the same id and the XHTML is no longer valid!
Do we have any other options while still using Struts tags?
If all you need to do is differentiate between the two forms, then just add a parameter to the action...
<html:form method="post" action="/foo.do?id=bar">
...
</html:form>
...
...
<html:form method="post" action="/foo.do?id=foo">
...
</html:form>
Then in your action, you just have to get the parameter from the request. Its been a while since I have used struts, but I had multiple forms going to the same action, and this is how I solved it.
1) For your xhtml compliance, rather do this:
<html:html xhtml="true">
</html:html>
The styleId="bar" is rendered to html as id="bar" and that's why you're getting an exception (because Structs generates the following in html)
<html:form method="post" action="/foo.do" styleId="bar">
to
<form id="foo" action"/foo.do" id="bar">
(Bear in mind that id="foo" depends on your declaration of <action name="foo" />). StyleId is used for element attribute purposes, e.g. styleClass="this" will render class="this"
A solution would be to add an id to the action, as in:
<html:form method="post" action="/foo.do?id=blah" styleId="bar">