PDA

View Full Version : Is it possible to Map /* to the DispatcherServlet in the web.xml file?



myZot
Aug 1st, 2006, 11:09 PM
e.g.
<servlet>
<servlet-name>myspring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myspring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

I know you can map /*.whatever or /whatever/* but if I map /* I get the following error when trying to access a jsp page using the InternalResourceViewResolver:

WARNING: No mapping for [/myspring/WEB-INF/jsp/internalview.jsp] in DispatcherServlet with name 'myspring'


ANY HELP GREATLY APPRECIATED!!!!

myZot
Aug 1st, 2006, 11:10 PM
If it isn't possible, can you please give me an explanation why?

I want to use Convention over configuration but I don't want to have to put a directory name in front of the request or end it in dot something so

www.myurl.com/dothis will execute the DothisController and
www.myurl.com/dothisalso the DothisalsoController

Steve O
Aug 2nd, 2006, 02:06 AM
You may want to double check your mapping, usually, you can map something like internalview.html (or whatever) to the /WEB-INF/jsp/internalview.jsp. In other words, your html extension will call the DispatcherServlet - the servlet will return a ModelAndView - usually, in a properties file, you will have the view point to the jsp... not a link.

Good Luck,

Steve O

myZot
Aug 6th, 2006, 09:51 PM
I understand that, but in this case, I want to map everything without an extension which I don't think is possible. I think it chokes the intervalviewresolver. What is interesting is that if I map /* in the web.xml, the controller will properly get executed, but the internalviewresolver fails and so does every other file view. All I have been able to sucessfully do is return a redirect view to a page outside the context.

I basically want to map all directory requests to a controller and succesfully display an internal view.

kwalton
Aug 7th, 2006, 01:27 PM
The only way I could get it to work is to either do a mappping like: "/*.do" or "/app/*".

The latter mapping was obviously better then the former but still adds an unnecessary part of the URI.

myZot
Aug 8th, 2006, 03:21 AM
I was just hoping for some kind of workaround.

Bjorn Johnson
Aug 8th, 2006, 10:16 AM
I think I'm confused because we're talking about different kinds of mappings like they're the same thing.

Doesn't the message "WARNING: No mapping for [/myspring/WEB-INF/jsp/internalview.jsp] in DispatcherServlet with name 'myspring'" mean that your dispatcher servlet can't figure out what to do with "/myspring/WEB-INF/jsp/internalview.jsp"? If so, I don't quite see what the servlet mapping in web.xml has to do with this. You can definitely use /* as your url-pattern for the servlet mapping for your dispatcher servlet. It just maps any url matching /* to go to the dispatcher servlet. If your dispatcher servlet is providing that warning message for you, you can be sure that the mapping in web.xml is working just fine.

If your goal is to "map all directory requests [all urls not ending with a .* ?] to a certain spring controller and succesfully display an internal view" you can probably configure something like SimpleUrlHandlerMapping and provide a specific mapping that points to the controller you want to use for that mapping. Are you using Spring configuration (i.e. xml) for your application? If so, can you provide your configuration -- specifically any mappings and the internalresourceviewresolver?

myZot
Aug 9th, 2006, 09:23 PM
Aug 9, 2006 7:21:39 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for [/myspring/WEB-INF/jsp/internalview.jsp] in DispatcherServlet with name 'myspring'


The following fails:

http://localhost/myspring/index.html


***********************************
WEB-INF/web.xml
***********************************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>MySpring</display-name>
<servlet>
<servlet-name>myspring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myspring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
***********************************
WEB-INF/myspring-servlet.xml
***********************************
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResou rceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="whatController" class="org.springframework.web.servlet.mvc.Parameterizabl eViewController">
<property name="viewName" value="internalview"/>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<props>
<prop key="/index.html">whatController</prop>
</props>
</property>
</bean>
</beans>
***********************************
WEB-INF/jsp/internalview.jsp
***********************************

myZot
Aug 9th, 2006, 09:25 PM
Now, if I change the web.xml to map /bob/*

e.g.

<servlet-mapping>
<servlet-name>myspring</servlet-name>
<url-pattern>/bob/*</url-pattern>
</servlet-mapping>

and I type

http://localhost/myspring/bob/index.html

it works!

So, again, my question: is it possible to map /* to spring?

Bjorn Johnson
Aug 10th, 2006, 10:56 AM
Maddening.

I know the /* mapping works for a servlet because I've done it with plain-old, non-Spring servlets and it works like a charm.

I remember another post somewhere about the DispatcherServlet being called more than you'd think when you use such a generic mapping as "/*". So maybe it is being called first when you make your request and again ("secretly") when Spring shows the view?

This might explain why you'd have gotten your error message about "
WARNING: No mapping for [/myspring/WEB-INF/jsp/internalview.jsp] in DispatcherServlet with name 'myspring'". The first time dispatcherservlet is called everything is dandy, but if it is being called again because of the generic "/*" mapping, the url would then be "/myspring/WEB-INF/jsp/internalview.jsp" (complements of the view resolver). Since there is no mapping for that, it craps out.

Just an idea.

myZot
Aug 10th, 2006, 06:55 PM
I think that is what is happening... That's why sometime it appears to be going into an infinite loop.

The question is: Is there a workaround?
Is there anyway to have the sercret view automatically route to the DefaultServlet? You would think you would want this anyhow. It almost sounds like a bug.

What I don't understand quite clearly is if WEB-INF/jsp is not accessible directly, how come the web.xml /* mapping is still being used?

A possible workaround would be if you could have order to the mapping and have a /WEB-INF/* mapping executed first and the /* mapping executed second. You could then map /WEB-INF/* to the Default Servlet, but I think /* just takes over?? any ideas?

Bjorn Johnson
Aug 11th, 2006, 10:45 AM
In terms of workarounds, I suppose you could set a more restrictive mapping to your dispatcherservlet (to avoid the circularness of the /* mapping) and then set a defaulthandler for your SimpleUrlHandlerMapping. The defaulthandler maps to a controller that's like a fallback controller -- it is called when no mapping exists for the given url. You would then have to program logic into that controller for dealing with the url, specifically if you want to forward it to a view (like in the case you first posted of "any time a user enters a directory path...") or determine if it is actually bad (like when someone enters a url for a resource that doesn't exist). Didn't really think this through -- just an idea.

Maybe there's some better built-in way to implement this. But that would require someone that knows what they're doing to chime in, which ain't me.

Rossen Stoyanchev
Aug 11th, 2006, 10:48 AM
If a container has an internal JSP container, the *.jsp extension is mapped to it allowing JSP pages to be executed on demand. However, using the "/*" pattern literally maps any request including those that would otherwise be covered by the implicit .jsp mapping.

One way to get around this would be to make DispatcherServlet the default servlet (use the "/" mapping). This will still forward every URI to the DispatcherServlet but will allow JSP pages to be executed on demand.


<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>