Spring MVC 3 - Need help in resolving the url-pattern issue - spring-mvc

I am developing an app with spring mvc and here is my web.xml
<servlet>
<servlet-name>admin</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
For serving static content, I added the following line in my admin-servlet.xml
<context:component-scan base-package="com.prasanna.blog.Server" />
<mvc:resources mapping="/resources/**" location="/"/>
I have 5 controllers each mapped to particular url for example adminRequests.java is mapped to /admin/*
#Controller
#RequestMapping("/admin/*")
public class AdminRequests
Then I have methods inside the class mapped to particular urls.
The problem is, I am getting 404 error when the url-pattern in web.xml is mapped as / .But when I change the url pattern in web.xml to /app or any other , my index.html is served without any issues.
I am not sure where the problem is. Please advice

When you map Url pattern / to Spring's Dispatcher Servlet, all the request to your application is forwarded to Dispatcher Servlet. Although in this case you defined the static resources using <mvc:resources> tag, but your index.html is not resolved properly because it is not rendered as /resources/index.html but /index.html.
I would suggest you to add one more <servlet-mapping> in your web.xml before spring's <servlet-mapping>. For example:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Hope that will work for you.

No that did not resolve the issue. But I saw another thread discussing the same issue and one of the answer there worked. I removed <mvc:resources> line from spring xml and added the below lines in the web.xml
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
Now my static content is served without any issues but I don't like this method. It feels like a very dirty hack. Now if I have to serve images, I have to add mapping for each image type. I am not sure whether the spring version is a problem because I use mongodb in the backend with spring data. It took a hell of a time to set up my pom.xml because there were some issues with the spring data version and mongodb driver.

Related

HTTP request with RESTful URL and .htm URL

URL with *.htm works fine. But I encounter a problem is when I try to access RESTful URL without *.htm, then the server gives me 404 error. Well I can solve the problem by replacing *.htm with /. If I do that then I break my webpages. So the question is how do I go about supporting RESTful and regular HTTP request through the same Spring Controller? What needs to be configured in Web.xml? I tried something like /rest/* but that does not seems to be working either.
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Tried
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Tested with configuration 'Type - I'
Works when URL is set to end with .htm http://localhost:8080/controllerName/my/123/url/value.htm
Web.xml mapping
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Controller Mapping
#RequestMapping(value = "/my/{number}/url/{somevalue}", method = RequestMethod.GET, produces="application/xml")
Test with configuration 'Type - II'
No other combination seems to work when URL does not contain .htm
This may be late, but it may be an issue with the DefaultAnnotationHandlerMapping in Spring. It will, by default I believe, automatically handle extentions, but that can be turned off. Also, your Servlet definition in your web.xml should be set to accept URLs ending in '*'.

Spring MVC eliminate *.html pattern

I use Spring MVC 3.1 with following front controller configuration:
<servlet>
<servlet-name>paymentSystemServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlet-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>paymentSystemServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Because I have pattern like "*.html" I have to do next thing. I need an html extension in all my url's to controller (e.g. 'a href=/admin.html' intercepts controller with request mapping '/admin'). Can I configure some url-pattern in order to avoid html extensions in my url? Thank you.
By mapping DispatcherServlet to /, you can use it as a default servlet and it’ll be responsible for handling all requests including html, htm etc.
<servlet-mapping>
<servlet-name>spitter</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Or you can use the Spring MVC 3.x default servlet handler to do the job. Just add that following to the Spring XML config.
<mvc:default-servlet-handler/>

Spring web application without redirection

I am trying to create a spring web application. The application should directly go to a controller and should not have any welcome file. What i am trying is
At the top of this controller I have the annotations
#RequestMapping(value = "/", method = RequestMethod.GET)
and my welcome file list has root context
<welcome-file-list>
<welcome-file>/</welcome-file>
</welcome-file-list>
So my application should run as :
localhost:7101/samplespring/
However , the problem is when I try to run the application in jdeveloper , I need to set a default target , which I obviously cannot here.
Hoow to proceed.
Thanks n Regards,
Rachit
I'd remove the <welcome-file-list> entry and ensure your servlet-mapping in web.xml looks something like (note <url-pattern>):
<servlet>
<servlet-name>mainDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mainDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

spring MVC servlet mapping

I am trying a sample spring MVC application. My web.xml has
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
so my question is how I can call my test controller if I just type in the url
http://localhost:8080/MySpringProject/test
Where do i need to make change to call exactly this type of URL so that my test controller called. I don't know what I am asking is correct or not but my requirement is that I don't want to end my UrL with "/" or "test.htm".
Please help and thanks in advance
You usually map your dispatcher servlet to /, and then you have controllers with #RequestMapping("/foo/bar"). But if you define a servlet with a more specific url, it will get picked up.

How to use wildcards in Spring MVC servlet mapping URLs?

I've got a controller set up in my web.xml:
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/console/index</url-pattern>
</servlet-mapping>
And a matching bean defined in controller-servlet.xml:
<bean name="/console/index" class="com.package.OverviewController"/>
Which works correctly - when I get "/appName/console/index" it behaves as I expect. But when I change web.xml to this:
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/console/*</url-pattern>
</servlet-mapping>
It no longer functions, giving me the following exception:
WARN (org.springframework.web.servlet.PageNotFound) - No mapping for [/appName/console/index] in DispatcherServlet with name 'controller'
So my question is how do I use wildcards in the servlet mappings, so that different URLs all go through the single DispatcherServlet but may go to one of several controller beans?
FYI: I'm stuck on Spring 2.0, as it's an established application used in government.
When you use wildcards in <url-pattern>, controller names by default correspond to wildcard part of the pattern.
So, you can either rename your controller to /index, or set the alwaysUseFullPath property of your HandlerMapping to true.

Resources