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.
Related
I want to use the xml configuration file for registering clients for OAUTH2 in my web application. I know that there exist support for xml tags using the client-registrations tag
However I want to register the client registration repository as a bean. So that it is extensible, something like this:
<beans:bean id="clientRegistrationRepository" class="org.springframework.security.oauth2.client.registration.ClientRegistrationRepository">
<beans:constructor-arg index="0" ref="clientRegistration"/>
</beans:bean>
... more tags expanding clientRegistration
However this does not work. Does any one know how we can do this ?
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"
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";
}
I have a sample application with web flows in a number of jars (extensions) and the configuration is loaded from the classpath this works fine. But I'm struggling with the view resolver configuration that will load the JSP's from the same location as the flow definitions on the classpath?
so I have the following folder structure on the classpath
MEAT-INF/config-webflow/ext1/ flow definition and JSP's for flow extension 1 are here
The web flow configuration works fine as I set this as flow-builder-services="flowBuilderServices" base-path="classpath*:/META-INF/config-webflow">
But as yet I can not come up with a view resolver configuration to load the JSP's
Any ideas?
I had the same problem. I discovered that you get the desired behaviour by NOT setting the normal Spring MVC 'viewResolver' bean on the MvcViewFactoryCreator.
<flow-builder-services id="flowBuilderServices" view-factory-creator="viewFactoryCreator" development="true" />
<beans:bean id="viewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator" />