I had made a basic application on spring mvc framework.
when i write the following url pattern on web.xml:
<servlet-mapping>
<servlet-name>springxml</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
and runs the application (using ../SpringMVCXML/welcome.jsp) , it displays HTTP Status 404 error.
When I changes the url pattern other than .jsp, application is running fine.
Why Application is not running on .jsp url pattern?
I had used following java class act as controller.
#Controller
#RequestMapping(value="/welcome",method=RequestMethod.GET)
public class ControllerHello {
#RequestMapping(method=RequestMethod.GET)
public String printHello(ModelMap map) {
map.addAttribute("message", "Hello Spring MVC Framework");
return "hello";
}
}
Also, my springxml-servlet.xml had following code:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
Assuming you have also springxml servlet is also serving other views than *.jsp (you might have other servlet-mappings in your web.xml), the situation is as follows:
You registered a controller for the path /welcome, not for /welcome.jsp. Therefore, /welcome.jsp is not mapped to a Spring mvc controller.
The /welcome HelloController will give you the String output hello.
/welcome.jsp will give an error 404, since it is in WEB-INF and there is no Spring MVC controller for that url.
The org.springframework.web.servlet.view.InternalResourceViewResolver is meant to put your jsp files inside /WEB-INF. Look at http://www.mkyong.com/spring-mvc/spring-mvc-internalresourceviewresolver-example/ for a simple explanation for what the InternalResourceResolver does. Basically, it enables Spring MVC to use a jsp, that is not in your public resources, as a view.
It is not a mechanism to register these jsps as valid urls.
The urls are determined in the requestmappings.
you probably do not have any spring controller mapping to this URL /welcome.jsp
what version of spring you are using? if you are using spring 2.5 or above, try following code
#RequestMapping(value = "welcome.jsp")
public String welcomeJSP(){
return "welcome";
}
Related
I'm using Spring MVC (Version 4.1) on Tomcat 8, and am desperately trying to make the file upload functionality work. Currently, I have a controller configured like this:
#RequestMapping(value={"/TestCase/Upload"}, method=RequestMethod.POST)
#ResponseBody
public ResponseEntity<String> uploadFile(HttpServletRequest request,
#RequestParam("file") MultipartFile file) {
System.out.println("Hit this location.");
return new ResponseEntity("Success");
}
My web.xml has the appropriate server configuration:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
<!-- Configuration for file upload (configuring Multipart file) -->
<multipart-config>
<location>/tmp</location>
<max-file-size>500000</max-file-size>
<max-request-size>505000</max-request-size>
<file-size-threshold>10485</file-size-threshold>
</multipart-config>
</servlet>
And finally, my Spring xml configuration file has the necessary resolver specified:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="500000" />
</bean>
The Apache commons-fileupload JAR is on the classpath.
I have used this approach successfully in the past on non-Tomcat servers, but now the application isn't working - but it's failing quietly. The response has a status code of 200, but nothing inside of the file upload controller method is executed. There are no exceptions thrown in the server logs, and the only way I can get the controller method to print anything out is if I remove the "Multipart" parameter entirely. At first I thought that the controller method wasn't being hit at all, but if I change the URL mapping, then the calling code throws a 404 - so it is definitely hitting the correct mapping/method - it's just that nothing inside of the method is executing (with no exceptions thrown!)
What am I doing wrong?
It turns out Spring MVC will hide noClassDef's from the console when booting itself up. The issue was that apache-commons-io.jar was missing on the classpath. Including that JAR caused everything to work properly.
So in the future if Spring is quietly misbehaving - check to ensure all necessary libraries are explicitly included, because it certainly won't tell you!
I m developing a spring web application .
I have put all my resources folder in webcontent folder and configured it in my dispatcher.xml
<mvc:resources location="/asset/" mapping="/asset/**" />
I have configured my startup page as following
<mvc:view-controller path="/" view-name="Framework/start"/>
My application is running fine and all the resources are also loading but not on the first run. Means when I deploy my application on tomcat7 and hit the url for the first time the css are not loaded also my href which is mapped to a controller is also not working but once I am logged in and logout everything works fine.
After lots of effort i concluded that the problem was not with the resource path but the problem was due to the interceptor . The authentication interceptor that i have added was called multiple times due to the request to the resources and as there was no session created till that time it was returning false.
Hence i exclude any calls to resources folder from the interceptor in the following way-
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/asset/**"/>
<bean class="com.model.AuthenticationInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
Also one imp thing mvc:exclude-mapping is added from spring 3.2 onwards so one need add the schema "http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
Have a sample app and created a
view/HelloWorld.html
page. From my controller, I return the following
public String home(Locale locale, Model model) {
return "HelloWorld";
}
In debug mode I get this warning/error:
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/HelloWorld/WEB-INF/views/HelloWorld.html] in DispatcherServlet with name 'appServlet'
contents of my src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".html" />
</beans:bean>
If I rename the .html to .jsp and change above to .jsp then things work fine.
The flow that the servlet container goes through for this request is the following:
First the DispatcherServlet is invoked by the Servlet Container.
The DispatcherServlet finds a mapping which maps to the home method of your Controller and the home method returns a view name "HelloWorld"
Now the DispatcherServlet uses a View Resolver (your InternalResourceViewResolver) to find the View to render the model through, since the name is "HelloWorld", this maps to the /WEB-INF/view/HelloWorld.html view.
Now essentially a call is made to RequestDispatcher.forward("/WEB-INF/views/HelloWorld.html",....
The Servlet container at this point tries to find the servlet which can handle /WEB-INF/views/HellowWorld.html uri - if it had been a .jsp there is a JSPServlet registered which can handle rendering the jsp, however for *.html there is no servlet registered, so the call ends up with the "default servlet", which is registered with a servlet-mapping of / which probably your DispatcherServlet is.
Now the Dispatcher servlet does not find a controller to handle request for /WEB-INF/views/HelloWorld.html and hence the message that you are seeing
If you want this kind of a extension to be handled by the servlet container, say tomcat, you can register *.html extension to be handled by JSPServlet and then it should work cleanly. Or return forward:/resources/HelloWorld.html which will be considered a static file relative to your resources folder.
There's a lot of difference between html and jsp. Java server pages are compiled into Java ‘servlets’. It may call beans and enterprise beans, such as Java Beans components and Enterprise Java Beans components, to execute processing on the server. So, having such JSP technology could be a key component in high-level architecture for web-based applications.
I am working on a Spring MVC project incorporating Tiles.
Here is the current urlPattern in the web.xml.
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
In the spring configuration file, currently the only view resolver is.
<bean id="viewResolver"
class="org.springframework.web.servlet.view.ResourceBundleViewResolver"
p:basename="views" />
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
p:definitions="/WEB-INF/tiles-defs.xml" />
In my controller, the only way the request mapping works is if I enter in /sample.html which routs to the proper controller. What I need to do is have the current view /sample to map to the proper controller. Is there a way to do this. Would internalviewresolver or an entry change in the views.properties file be the answer? Thanks.
No, you have to change the servlet url-pattern. Map it to /
I just switched from Striped to Spring but i'm having issues with my very first project,
Basically i get the 404 from the server.
Strange enough, i have followed one by one all the steps in my book.
I use Eclipse, Tomcat 6 and Spring 2.5
The structure of my project is like so:
src>
controllers(package)>SpringTestController(implements controller).....then
......web content>jsp(folder)>hello.jsp.....then....web-content>web-inf>SpringTest-servlet.xml and web.xml
inside lib i have the 9 necessary jars.
my controller:
public class SpringTestController implements Controller{
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
return new ModelAndView("jsp/hello.jsp");
}
}
my SpringTest-servet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/hello.htm" class="controllers.SpringTestController"/>
</beans>
my web.xml(without header to save space)
<servlet>
<servlet-name>SpringTest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringTest</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Where do you think the problem could be?
I have been trying to look around the files but beside web.xml "where i dont see any abnormality" i'm very new to this flow structure so i really cannot get where the problem is.
Thx for your time
I'm suspicious of the practice of using the name attribute of bean to specify URL paths - while I'm sure it's probably possible, my answer will tell you how to do it using more traditional means.
First of all, here is the new SpringTest-servlet.xml:
<bean id="helloController" class="controllers.SpringTestController" />
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">helloController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
You'll probably notice a few things. I'm using the id attribute of bean to define your controller so it can be referenced elsewhere (in the urlMapping bean as you're about to see).
I define a urlMapping bean, which does exactly what you think it does - maps requests (e.g. /hello.htm) to a controller bean.
I've also used viewResolver to map views names to view files, however that is a personal preference thing. Since I'm now using a view resolver, your controller looks like this:
public class SpringTestController implements Controller {
#Override
public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
return new ModelAndView("hello");
}
}
I don't need to put the path to the view because the viewResolver prepends it with "/WEB-INF/jsp/" and adds ".jsp" to the end. You can change the prefix to wherever you store your view files, or you can not use it at all. It's a personal preference thing, though I like to use it :)
Sorry if this answer didn't conform to your style - I tried to get it working your way and couldn't, so this is how I normally set up a Spring project (if I'm not using annotations).
Hope this helps.