SimpleUrlHandlerMapping not working for url with few extentions like dsm or ds - spring

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.

Related

almost identical webapps behave differently in serving static content

I'm having a 'weird' issue in serving static content from my webapp using a Spring-Hibernate setup.
I have a (small) webapp that uses the DispatcherServlet to serve any url like this: /coverpage/* .The default servlet mapping is set to /static/*
Example:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/coverpage/*</url-pattern>
</servlet-mapping>
In my jsp's the static content (images/js/css) is written like this:
<spring:url value="/static/styles/js/jquery-1.4.2.min.js" />
I have 4 pages in the webapp: index.jsp, new.jsp, error.jsp and includes.jsp (which isn't an actual page, duh) and a mapping for /process which returns PDF content or validation-errors when entered incorrect data.
This setup seems to work fine in my webapp.
Now for the weird part:
I have deployed the same webapp on another Tomcat instance and changed the ports so they can run simultaneously and now the references to static content are only working half the time. Only the mapping for /new (aka new.jsp) can find the static content. The rest fails to load it.
Differences between the 2 instances
Tomcat version (original runs on 6.0.29, second instance runs on 6.0.33)
Tomcat ports
Database connection (original goes to PROD db, second instance goes to TEST db - both databases are the same)
Here's my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Coverpage creator</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/coverpage/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>SetUTF8EncodingFilter</filter-name>
<filter-class>be.coverpagecreator.filter.SetUTF8EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SetUTF8EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>Coverpage creator</web-resource-name>
<url-pattern>/coverpage/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>cpcreator</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Coverpage creator</realm-name>
</login-config>
<security-role>
<description>The role that is required to create coverpages</description>
<role-name>cpcreator</role-name>
</security-role>
</web-app>
And dispatcher-servlet config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="application-context.xml"/>
<mvc:annotation-driven />
<mvc:interceptors>
<bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory" />
<property name="singleSession" value="false" />
</bean>
</mvc:interceptors>
<context:component-scan base-package="be.coverpagecreator" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="defaultEncoding" value="UTF-8" />
<property name="useCodeAsDefaultMessage" value="false" />
<property name="basenames">
<list>
<value>/WEB-INF/messages/global</value>
<value>/WEB-INF/messages/messages</value>
</list>
</property>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver" />
</beans>
The problem was with the Tomcat version. I tried to run the second instance on a newly downloaded 6.0.29 Tomcat instead 6.0.33 and it seems to work fine. I'm gonna go through the Tomcat changelogs to try and find what caused this... If I find anything, I'll report back here.

What are the configurations or xml files to run a basic Spring MVC application?

I need to know the basic XML configuration files to put in my Web-inf folder, like web.XML, and where to write view resolver bean.
How do I register the controller?
When I run my application I get the error "could not open servlet context resource". What is servlet context resource and where do I mention it?
My web.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
jsp/index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
My springmvc is:
<?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_world.html" class="springmvc.web.HelloWorldController"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
I have these two xml files only.
I know I need a spring configuration file too. I don't know what details I need to put in that.
If you use STS (Spring Tool Suite) then you can create a Spring Template Project.
(New Project/SpringSource Tool Suite/Spring Template Project/-Spring MVC Project)
This contains all the files you need to start.

configure BasicDataSource as bean in web.xml

I'm trying to configure org.apache.commons.dbcp.BasicDataSource as bean in web.xml under a tomcat project using tomcat 6. (it's red5 with tomcat, we can ignore that the main server is actually red5 because i actually run jsp files under port 5080 and don't connect to the red5 directly using RTMP protocol)
my web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/ j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>gamesisland-login-red5</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>/[myapp]</param-value>
</context-param>
<listener>
<listener-class>org.red5.logging.ContextLoggingListener</listener-class>
</listener>
<filter>
<filter-name>LoggerContextFilter</filter-name>
<filter-class>org.red5.logging.LoggerContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoggerContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>rtmpt</servlet-name>
<servlet-class>org.red5.server.net.rtmpt.RTMPTServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/fcs/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/open/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/close/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/send/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/idle/*</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>Forbidden</web-resource-name>
<url-pattern>/streams/*</url-pattern>
</web-resource-collection>
<auth-constraint/>
</security-constraint>
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/red5-web.properties" />
</bean>
<bean id="idDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${db.driver}</value></property>
<property name="url"><value>${db.url}</value></property>
<property name="username"><value>${db.username}</value></property>
<property name="password"><value>${db.password}</value></property>
<property name="poolPreparedStatements"><value>true</value></property>
<property name="maxActive"><value>10</value></property>
<property name="maxIdle"><value>10</value></property>
</bean>
</web-app>
my red5-web.properties:
webapp.contextPath=/myapp
webapp.virtualHosts=*
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/dbname
db.username=user
db.password=pass
does tomcat automatically searches for WEB-INF/web.xml for configuration?
why don't I get any relevant errors to the creation of idDataSource ?
I really don't have any clue how to pinpoint or debug the problem.
any assistance would be greatly appreciated.
thank you!
kfir
I don't know nothing about Red5, but it seems like you trying to put Spring beans directly inside web.xml, which is wrong. You are suppose to create a separate Spring configuration file that will be picked up by Springs' ContextLoaderListener. First, add this to your web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Than create applicationContext.xml file under /WEB-INF:
<?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-3.0.xsd">
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/red5-web.properties" />
</bean>
<bean id="idDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${db.driver}</value></property>
<property name="url"><value>${db.url}</value></property>
<property name="username"><value>${db.username}</value></property>
<property name="password"><value>${db.password}</value></property>
<property name="poolPreparedStatements"><value>true</value></property>
<property name="maxActive"><value>10</value></property>
<property name="maxIdle"><value>10</value></property>
</bean>
</beans>
Of course all the <bean/> declarations should go away from web.xml.
Second thought: looking at this document it seems like Red5 uses a file named red5-web.xml, please go through this documentation carefully.

Tomcat unable to find jsp in war file

I recently switched one of my static html files to a Spring controller that uses a JSP to render its view. I use jetty to test locally and local testing shows the page rendering fine. Upon deploying to our test server, which uses Tomcat 6.0.26, I get the following exception:
javax.servlet.ServletException: Could not get RequestDispatcher for [/WEB-INF/jsp/index.jsp]: check that this file exists within your WAR
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:219)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
I have confirmed that the JSP exists in the war I deploy and the exploded directory that tomcat creates upon deployment. Here is what my web.xml and front-controller-servlet.xml files look like respectively (web.xml shorted slightly):
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Initialize the Spring DispatcherServlet -->
<servlet>
<servlet-name>gwtrpc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map the DispatcherServlet to only intercept RPC requests -->
<servlet-mapping>
<servlet-name>gwtrpc</servlet-name>
<url-pattern>*.gwtrpc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>front-controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>front-controller</servlet-name>
<url-pattern>/search.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>front-controller</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
front-controller-servlet.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/search.html">indexController</prop>
</props>
</property>
</bean>
<bean id="indexController" class="com.company.search.web.server.controller.IndexController" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>
The difference between my dev instance and test instance is that the dev instance is deployed to the root (localhost/search.html) whereas the test instance is deployed to server.com/appname/search.html. Appname is the name of the *.war file I deploy. I have tried adding the full path as the prefix to the jps files (/appname/WEB-INF/jsp/) and a number of other combinations with no luck. I have verified that the jsp-api jars are in the tomcat lib directory. My tomcat install is the basic/default install.
The issue was caused by the default jsp servlet being removed from the default web.xml of the tomcat install. This had been done by a former co-worker and not to my knowledge. Replacing the test server's web.xml with the default web.xml (which included url-patterns/servlets for handling jsp files) corrected the issue.

UrlFilenameViewController does not return View (Spring-MVC)

According to Spring framework API
UrlFilenameViewController's purpose is:
Transforms the virtual path of a URL into a view name and returns that view
If i request for /info.xhtml, its logical view name is info
See UrlFilenameViewController documentation
Both web.xml and controller is mapped according to
/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8">
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>smac</display-name>
<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>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
/WEB-INF/controller-servlet.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 class="org.springframework.web.servlet.view.InternalViewResolver">
<property name="prefix" value="/WEB-INF/output/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
</beans>
My smac app deploys fine.
So, if i request for http://127.0.0.1:8080/smac/info.xhtml, Spring-MVC should return /WEB-INF/output/info.jsp. But i have seen in console the following:
No mapping found for HTTP request with URI [/smac/info.xhtml] in DispatcherServlet with name 'controller'
Could anyone help me ?
regards,
You need to configure a HandlerMapping. Default one is BeanNameUrlHandlerMapping which will only do what you want if you bind your controller bean under the name matching your URI. You probably want the SimpleUrlHandlerMapping instead:
<bean name="myController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/**/*.xhtml=myController
</value>
</property>
</bean>

Resources