Spring 4 form submission - java

I recently upgraded Spring framework from 3.1.2 to 4.1.1. Also upgraded to Tomcat 8 and Java 8. I am also using Tiles 2.2.2.
My web page loads fine using Spring 4 and problem comes when I do a form submission. The URL request changes and leaves out the webapp name.
For e.g., when I do a form submission,
the URL that is expected is supposed to be http://xx.xx.xx.xx/webappname/createuser/submit.
But the URL changes to http://xx.xx.xx.xx/createuser/submit. And thus, throws a "Requested resource is not available " error.
I did not have this problem when I was using Spring 3.1.2, Tomcat 7, Java 7.
May I know what I am missing?
Thank you,
Whiskers
EDIT :
My jsp view goes like
<form:form method="post" action = "/createuser/submit" commandName = "createForm" >
.....
< /form>

Prepend your hyperlinks with:
${pageContext.request.contextPath}
See the accepted answer here

Your action URL is start with root change your action URL to
action = "createuser/submit"
Or used <c:url ... /> tag to create URL and give it to action as shown below
<c:url value="createuser/submit" var="myActionUrl" />
<form:form action="${myActionUrl}" .... >
May this will help you.

Related

weird issue : jsf inside jsp not working

I have created web-application using JSF 2.0 & JSP and facing some weird problem.
I have page in JSF where I have included JSP page. code are as below.
<o:resourceInclude path="detailedReports.jsp" />
Where o is omnifaces. xmlns:o="http://omnifaces.org/ui"
Now in JSP (detailedReports.jsp) I have code as below.
<h:form id="myForm" prependId="false">
<h:commandLink value="Take Me To Some Page" action="#{PersonalInformationDataBean.moveToApplicantRegisterPage()}" />
</h:form>
moveToApplicantRegisterPage() have below code
public String moveToApplicantRegisterPage() {
editedData = 1;
return takeMeToAnotherPage("registerForPatentss");
}
When I click on Take Me To Some Page link, I get directed to detailedReports.jsp and not to registerForPatentss.xhtml.
Any reason why this is happening? What should I do to get redirected to registerForPatentss.xhtml.
Note: While redirecting to registerForPatentss.xhtml, I also need to set the data of int editedData to 1.
It might be a rendering problem related to mixing two different view technologies (JSP and facelets).
I don't think there is any solution. So what I did is, take JSP in JSF i.e. I re-wrote JSP content in JSF Format. So now I don't have any JSP page.
All is working perfectly!!!

http status 404 : the required resource is not found

I creat a web application using eclipse and tomcat7 I had the following code in the html file and the java servlet class
in the html file:
<form action="UserAccessServlet" method = "get">
in the servlet class I had
#WebServlet ("/UserAccessServlet")
then I just made some small changes (new println statements) but it shows no effect I changed the server name with the following peice of code
html file: <form action="SQA_Servlet" method = "get">
java class: #WebServlet ("/SQA_Servlet")
but it seems that no reload take place and I got the following error:
HTTP Status 404 - /SQA_Learning/SQA_Servlet
--------------------------------------------------------------------------------
type Status report
message /SQA_Learning/SQA_Servlet
description The requested resource (/SQA_Learning/SQA_Servlet) is not available.
I tried clean the module, refresh, close the reopen the project with the same result
I replaced #WebServlet ("/SQA_Servlet") with #WebServlet(urlPatterns={"/SQA_Servlet"})
and still have no effect.. any suggestion.
The WebServlet name attribute cannot start with a /. Rather do,
#WebServlet("UserAccessServlet")
or leave it blank (if you want the WebServlet to use the name of your Servlet class name. Example:
#WebServlet
public class UserAccessServlet extends HttpServlet {
//Do stuff
}
I would recommend declaring your WebServlet annotations fully like in this example.
I'm not sure when and in what for conditions you are receiving this error. But if you are deploying to tomcat, the following might occur:
Assuming your webapp is called "my.webapp" resulting in my.webapp.war
assuming you have a Servlet "servlet1" which performs action1 => #WebServlet(urlPatterns = "/action1") (note the slash in front of action1)
Assuming you are calling this action with a html form:
<form action="/action1" method="GET"> this might not work because of the slash in front of action1
When it's there tomcat will redirect to localhost:8080/action1?..
while it should redirect to localhost:8080/my.project/action1?..
Solution alter the html so the form looks like:
<form action="action1" method="GET">, don't change the #WebServlet(urlPatterns = "/action1")
Hope this helps someone!

How do I send data between servlets?

I am pretty new to servlets and web development in general.
So basically I have a servlet that queries a database and returns some values, like a name. What I want is to turn the name into a link that opens a details page for that name (which another servlet would handle). How can I send the name to the other servlet so it can query a database for the relevant details?
Maybe I'm taking the wrong approach?
Edit: I am using Tomcat 5.5
Pass it as request parameter.
Either add it to the query string of the URL of the link to the other servlet which is then available by request.getParameter("name") in the doGet() method.
link
Or add it as a hidden input field in a POST form which submits to the other servlet which is then available by request.getParameter("name") in the doPost() method.
<form action="otherservlet" method="post">
<input type="hidden" name="name" value="${name}" />
<input type="submit" />
</form>
See also:
Servlets info page - contains a Hello World
Not sure if I understand correctly, but you may look at javax.servlet.RequestDispatcher and forward the url to the second servlet.
The url could be created using the name:
http://myhost.mydomain/my.context/servlet2.do?name=John
I would create the URL either in the first servlet or in a client using a configurable template for the URL. This way both servlets are clearly separated - you can even have each one on different machine.

How do we get the absolute path to the applications root directory using Spring?

I have an app that may run at http://serverA/m/ or http://serverA/mobile/. I have a shared header with a search form that needs to go to http://serverA/installationName/search.
However, if I use <form action="/search"> it goes to the root of the server, not the tomcat application.
If I use <form action="search"> it goes to a path relative to the current page. (i.e http://serverA/m/someOtherPage/search
I've tried <c:url value="search"> and <c:url value="/search"> but neither of them seem to work.
In intelliJ, <c:url value="/search"> gives me "Cannot resolve controller URL /search" even though I have a controller defined with #RequestMapping("/search")
<form action="<c:url value="/search" />" />
Using <c:url> is the way. Ignore what the IDE tells you. They are not good at that. Just try to run it.
Bozho is right. I have used HTML BASE tag too:
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/" />
If you can put this tag in a few places (ideally in only one JSP) you can get your code cleaner.
You can (apart from other responders hints) also use Spring JSP tag (spring:url) which is modeled after the JSTL c:url tag (see Bozhos reply). The tld reference:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/spring.tld.html#spring.tld.url
And the bottom of this mvc:resources block for an example use:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources
you will not be able to imbed the c:url tag directly in the attribute, if your form tag is a jsp tag (perhaps, <sf:form>).
In that situation I do the following:
<c:url var="someName" value="some uri value"/>
<sf:form path="${someName}" ...>

Spring and dynamic inclusion of JSP files

I'm starting building web apps in Spring 3 (and in J2EE) in general.
Looking at the petclinic example I've seen that the programmer creates many JSP pieces, like header, includes, footer and then stitches them together using static inclusion. Anyway what I'd like is that I may have a base page, like Base.jsp and be able to include things like this:
<body>
<jsp:include page="${subpage}"></jsp:include>
</body>
the reason is that I'd like a main page, then being able to put in the ModelAndView returned by the controller which parts of the pages display in each situation (with the data attached to it). This works, but it gives no errors in case ${subpage} is not found, the jsp name is wrong or missing. I'd like more error checking...
Is this the best and recommended way to do this? And if this seems a good idea for what I've in mind, what's the correct way of doing it?
You might want to use Apache Tiles 2 integration for managing your JSP files. Spring has good integration support Apache Tiles. It also shows if there's an error in your page. I've put an example of it at http://krams915.blogspot.com/2010/12/spring-mvc-3-tiles-2-integration.html
It appears you have additional quotes in your subpage. Get rid of them. For example:
<c:set var="subpage" value="/jsp/index.jsp" />
If you have to set it in a controller or servlet - just use request.setAttribute("subpage", "/jsp/index.jsp")
For error checking you can use:
<c:catch var="myException">
<c:import url="${subpage}" />
</c:catch>
and later you can check it with:
<c:if test="${myException != null}">
...
</c:if>
Take a look at Sitemesh (http://www.opensymphony.com/sitemesh). It is a servlet filter-based page layout system that is easy to use. I have done a number of projects using it with Spring MVC and it worked very well.

Categories