Mapping to a JSON method with url-pattern - ajax

I'm creating a Spring MVC application that will have a controller with 'RequestMapping'-annotated methods, including a JSON method. It currently has static content that resides in webapps/static, and the app itself resides in webapps/myapp. I assume that Catalina's default servlet is handling the static content, and my *.htm url-pattern in web.xml is returning the request for my JSP page, but I haven't been able to get the JSON method to be called. How do I write the url-pattern in the servlet mapping to do so? Using /* has not worked; it prevents the app from being accessed at all. Is there anything else to be aware of?

I've learned of the default url-pattern, '/', which appears to be a match for my JSON request.

Related

Spring MVC - generic HTTP handlers

Have searched around and have not found a conclusive answer to this.
I have trying to route all http requests through my dispatcher servlet, and then onto a specific controller. Ultimately I want to be able to handle resource, AJAX and a.n.other request through the central point.
I currently have the url mapping /* in place to do this. My controllers use #RequestMapping("/[My resource].*") to capture my .htm requests. Unfortunately Spring appears to use RequestDispactcher.forward to resolve the .jsp from the InternalResourceViewResolver which is then hitting the front controller again and ultimately causing a 404 error.
My question is, am I able to setup a generic catch all that will handle any HTTP request other than the regular view request ?
The HTTP handler must be able to pass requests on to other servers and resolve internal and external resources e.g. images, css etc.
Regards,
Andy
Regards
A think a better idea is to change the servlet-mapping of DispatcherServlet to / instead of /*, this is because /* makes all request come to this servlet, instead like you have found for the jsp forwards also, inspite of the fact that there is a JSPServlet mapping for the jsps, the / mapping on the other hand will be defaulted to only if a specific mapping is not found for the requested path.
Keep the app servlet mapping to / in web.xml. Like shown below.
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
To resolve other resources add following tag in your dispatcher servlet xml.
Here resources is the folder containing js, css, images. It is stored under Webcontent folder in maven web application structure. Change it according to your project structure.
<resources mapping="/resources/**" location="/resources/" />
Try this.

Spring MVC 3.0 - restrict what gets routed through the dispatcher servlet

I want to use Spring MVC 3.0 to build interfaces for AJAX transactions. I want the results to be returned as JSON, but I don't necessarily want the web pages to be built with JSP. I only want requests to the controllers to be intercepted/routed through the DispatcherServlet and the rest of the project to continue to function like a regular Java webapp without Spring integration.
My thought was to define the servlet-mapping url pattern in web.xml as being something like "/controller/*", then have the class level #RequestMapping in my controller to be something like #RequestMapping("/controller/colors"), and finally at the method level, have #RequestMapping(value = "/controller/colors/{name}", method = RequestMethod.GET).
Only problem is, I'm not sure if I need to keep adding "/controller" in all of the RequestMappings and no matter what combo I try, I keep getting 404 requested resource not available errors.
The ultimate goal here is for me to be able to type in a web browser "http://localhost:8080/myproject/controller/colors/red" and get back the RGB value as a JSON string.
You are not correct about needing to add the entire path everywhere, the paths are cumulative-
If you have a servlet mapping of /controller/* for the Spring's DispatcherServlet, then any call to /controller/* will be handled now by the DispatcherServlet, you just have to take care of rest of the path info in your #RequestMapping, so your controller can be
#Controller
#RequestMapping("/colors")
public class MyController{
#RequestMapping("/{name}
public String myMappedMethod(#PathVariable("name") String name, ..){
}
}
So now, this method will be handled by the call to /controller/colors/blue etc.
I don't necessarily want the web pages to be built with JSP
Spring MVC offers many view template integration options, from passthrough to raw html to rich templating engines like Velocity and Freemarker. Perhaps one of those options will fit what you're looking for.

I do not want to map all my xhtml pages by a controller

But I want to use Spring security.
I think i have to use DispatcherServlet and its configuration in web.xml
I am developing an application that is nor jsp nor jsf project, i am going to make all connection based on javascript/ajax/jquery via server communication.
Thus i do not want to map my xhtml pages to a controller.
But i have a single controller with #RequestMapping(/auth/login) i only want it to run when i request /auth/login this is not the problem, it is working excellently.
But when i use
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:META-INF/spring-servlet.xml
1
spring
/heythere/*
and call http://localhost:8080/app/myhtml.xhtml it tells me i have no mapping for this uri.
I do not want mapping, nor controller to run, only want to see the page.
But DispatcherServlet needs to map it, how can i tell DispatcherServlet not to map ordinary xhtml pages?
Option 1:
Inside your spring web mvc application context XML you should put something like:
<mvc:view-controller path="/myhtml.xhtml"/>
The downside is you'll have to do this per page.
Option 2:
Use a Resource Handler:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/static/**"/>
Your page would be visible like http://localhost:8080/app/static/myhtml.xhtml.
More info can be found in Spring's Doc.

JSP Including A Private Servlet

I have a servlet that responds to a URL and then forwards to a JSP in a typical MVC pattern.
Many pages share the same page head so in the JSP there is an include to head.jsp
head.jsp is placed inside WEB-INF so that it cannot be accessed directly.
Now I find that I need to add some control to the head. Rather than forwarding to WEB-INF/head.jsp and putting scriptlets in I would like to forward to a servlet instead.
How can I forward from the JSP to a servlet without mapping that servlet to a URL as I do not want to give direct access to this servlet.
Or to put it another way is there a servlet equivalent of WEB-INF to hide it from direct access? So the servlet can only be called via an include?
Rather than forwarding to WEB-INF/head.jsp and putting scriptlets in I would like to forward to a servlet instead.
It's indeed possible to do this (using <jsp:include> or a small scriptlet that dispatches), but I'm not sure whether this is really the best approach. The Servlet would either write directly to the response or would put some data in the request scope that the JSP can pick up later.
Writing directly to the response is a bit debatable today and for the other approach you don't need a Servlet at all.
The idiomatic way is to use some helper bean that contains the logic. The original Servlet you mentioned can put this bean into request scope, or you can use the <jsp:usebean> tag. Reference the data the helper bean prepared via expression language or very simple scriptlets.
So the servlet can only be called via an include?
If you still want to go this route, there might be an option of securing the Servlet behind a role and then giving the head.jsp a run-as role in web.xml:
<servlet>
<servlet-name>headInclude</servlet-name>
<jsp-file>/WEB-INF/head.jsp</jsp-file>
<run-as>
<role-name>SYSTEM</role-name>
</run-as>
</servlet>
disclaimer: I have never tried this myself, just pointing in a possible direction.

Change header at Included JSP with Spring MVC

It is very interesting for me but I have simple Spring MVC application and JSP page. At Jsp pages which are included I would like to add a cookie to my application. However despite of setting it, It could not resolved at runtime.
this is the code at my included jsp page.
<% response.addCookie(new Cookie("test3","test3")); %>
I prefer writing some of the parts of the our application at jsp level over writing at controller.
What I can just say is that I am using Tuckey UrlRewrite and at instead of my jsp pages when I call my method, it is working fine. And at my called method I can see that the inital response object at my MVC controller is wrapped by another HttpServletResponse object. It seems that headers and cookies could not be changed after forwarded to jsp?
Any help?
PS: I have updated my question to make it clear regarding it is jsp included page.
JSP is part of the response. You need to ensure that that line is exeucted before the response is been committed. Otherwise you end up with IllegalStateException: response already committed in the server logs. So put it in the very top of the JSP page, before any HTML is been sent to the response. Or, better, just put it in a Spring controller or a servlet or filter, far before the forward to JSP takes place.
You also need to ensure that you aren't altering the response inside a JSP file which is included by <jsp:include>. It will be plain ignored. See also the RequestDispatcher#include() javadoc:
The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.

Resources