Under Tomcat 9 src/main/webapp folder, I created a sub-folder named app containing an index.jsp file. If I request localhost:8080/app I get a 404 error, while with a trailing slash localhost:8080/app/ it works fine.If I disable spring mvc the problem disappears. Tomcat logs with Spring MVC enabled:With trailing slash
21:17:33.351 [http-nio-8080-exec-7] DEBUG o.s.web.servlet.DispatcherServlet.traceDebug(91) - GET "/app/", parameters={}
21:17:33.351 [http-nio-8080-exec-7] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping.getHandler(414) - Mapped to ResourceHttpRequestHandler ["/"]
21:17:33.352 [http-nio-8080-exec-7] DEBUG o.s.web.servlet.DispatcherServlet.logResult(1131) - Completed 304 NOT_MODIFIED
Without traling slash:
21:17:57.046 [http-nio-8080-exec-9] DEBUG o.s.web.servlet.DispatcherServlet.traceDebug(91) - GET "/app", parameters={}
21:17:57.047 [http-nio-8080-exec-9] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping.getHandler(414) - Mapped to ResourceHttpRequestHandler ["/"]
21:17:57.048 [http-nio-8080-exec-9] DEBUG o.s.w.s.r.ResourceHttpRequestHandler.handleRequest(487) - Resource not found
21:17:57.048 [http-nio-8080-exec-9] DEBUG o.s.web.servlet.DispatcherServlet.logResult(1131) - Completed 404 NOT_FOUND
My web.xml dispatcher servlet section:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
My webapp context:
<mvc:resources location="/" mapping="/**"></mvc:resources>
<mvc:annotation-driven>
<mvc:path-matching trailing-slash="true" />
</mvc:annotation-driven>
<context:component-scan base-package="com.example.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/spring/views/" />
<property name="suffix" value=".jsp" />
</bean>
How can I configure Spring MVC to automatically add '/' in a path request (E.g. /app)?
Letting Tomcat serve static resources solves my problem. I removed Spring's static resources handler:
<mvc:resources location="/" mapping="/**"></mvc:resources>
and added the default servlet handler in order to let Tomcat handle requests of static files:
<mvc:default-servlet-handler/>
Related
I have one application that is using Spring framework version 3.2.9.RELEASE and upgraded to 5.3.22 and With this, I have not made any configuration changes so far.
My DispatcherServlet.xml and all the web.xml configurations are as is.
But before this upgrade, I have a controller with #RequestMapping("/ssoLogin") and if I use the URL "ssoLogin.html", it was able to find the controller, but after this upgrade it is not able to do so.
Below is my web.xml dispatcher server config
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>/auth/*</url-pattern>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Below is the view resolver configuration
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
<!--To maintain HTTPS state -->
<property name="redirectHttp10Compatible" value="false" />
</bean>
Till now no config change and the URL ending with .html extension was working fine and after upgrade it is not working and getting 404 not found error in browser.
Thank you #M. Deinum
Your comment helped me.
To solve this problem, I went though the below links
https://github.com/spring-projects/spring-framework/issues/24179
https://github.com/lamsfoundation/lams/commit/7ff6d4b34cd71ac45741cb7c8d0c3ef6909eadf4
https://github.com/spring-projects/spring-framework/issues/23915#issuecomment-563987147
And based on this I made below change in my dispatcher-servlet.xml file
<mvc:annotation-driven>
<mvc:path-matching suffix-pattern="true" />
</mvc:annotation-driven>
And this has solved my problem.
I am trying to implement SimpleUrlHandlerMapping in Spring. I am using Spring 4.2.5 version.
Following is my mapping
<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.xsd">
.....
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello1.dsm">hc</prop>
</props>
</property>
</bean>
<bean id="hc" class="com.vaannila.HelloWorldController" >
<property name="message" value="Hello World!" />
</bean>
....
</beans>
When I run tomcat, I get the info message on console saying
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/hello1.dsm] onto handler 'hc'
But When I hit the url "http://localhost:8080/SpringExample5/hello1.dsm" in my browser I get the requested resource is not available error i.e 404.
Later when I change key to "/hello1.htm", it worked fine with respective url. I am wondering is there any rule on url extention while mapping url to controller.
In web.xml, The dispatcher servlet url pattern is configured to .htm extention
<?xml version="1.0" encoding="UTF-8"?>
............
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>............
and hence It is working fine for ".htm" extention.
To make it work for other extentions we need to set corresponding url-pattern in web.xml file.
I am using Spring MVC in my building block.
I am getting java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet error when I check in blackboard logs and UI is displaying following error message -
The specified resource was not found, or you do not have permission to access it.
My web.xml config is -
<servlet>
<servlet-name>main</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</spring>
My view resolver config is -
<bean>
<bean id="primaryViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
My permission config in web.xml is -
<permission type="java.lang.reflect.ReflectPermission" name="suppressAccessChecks" />
<permission type="java.lang.RuntimePermission" name="accessDeclaredMembers" />
<permission type="java.lang.RuntimePermission" name="createClassLoader" />
<permission type="java.lang.RuntimePermission" name="setContextClassLoader" />
<permission type="java.io.FilePermission" name="${java.home}/lib/*" actions="read" />
You could include spring-beans and spring-context jars in your library. Also, the necessary jars might be already in your classpath but possibly they are not deployed on tomcat.
Be sure you are putting your JSP at the top level of your Building Block. Most often this error is caused by the dispatcher not being able to fine the jsp to render. If it is not at the top-level, just change your prefix property to point to the appropriate directory ().
Check https://github.com/blackboard/spring-b2-example for a very basic example to start with.
Spring MVC: I have my jsp files in path WEB-INF/jsp. I want to get the url
_http://mydomainname:8080/igloo/sports/scoreboard
to resolve using spring mvc. The jsp page is under /WEB-INF/jsp/sports/scoreboard.jsp
Here are the details of the installation, the error I am getting the at the bottom:
WEB.XML:
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
springapp-servlet.xml:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
#RequestMapping({"/sports/scoreboard"})
public String scoreboard(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response)
{
return "sports/scoreboard";
}
System File Path:
WEB-INF/jsp/sports/scoreboard.jsp
Browser Url:
http://localhost:8080/igloo/sports/scoreboard
Error:
HTTP Status 404 - /igloo/sports/WEB-INF/jsp/sports/scoreboard.jsp
Simply add a leading / to your InternalResourceViewResolver's prefix.
<property name="prefix" value="/WEB-INF/jsp/"></property>
Internally, the JstlView uses HttpServletRequest#getRequestDispatcher(String) to locate the jsp. Note the javadoc
The pathname specified may be relative, although it cannot extend
outside the current servlet context. If the path begins with a "/" it
is interpreted as relative to the current context root.
So if you have no leading /, it is relative to your current path, which happens to be /sports, so
sports/WEB-INF/jsp/sports/scoreboard.jsp
for which your application has no mapping and therefore returns a 404.
I develop a simple Spring application which is my university task. There are 3 configuration files: web.xml, core-context.xml, dispatcher-servlet.xml and 1 file with default properties which is called messages.properties and is located in /WEB-INF/ folder.
In my application I have the following configuration of ReloadableResourceBundleMessageSource and it works fine:
core-context.xml
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames" value="/WEB-INF/messages" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:core-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
But it turned out that my task instruction says that I should configure ReloadableResourceBundleMessageSource bean in dispatcher-servlet.xml. The problem is that whenever I remove the above configuration from core-context.xml and put it in dispatcher-servlet.xml my locals are no longer displayed.
Could you explain to me why the problem occurs?
What is a difference between putting bean configuration inside core-context.xml and dispatcher-servlet.xml?
You have not posted your dispatcher-servlet.xml but I believe it is not being initialized by the spring container because it is not declared anywhere. If you must have another file called dispatcher-servlet.xml as you specify then you can just import it in your core-context.xml. This should solve your problem.