问题
This is something that I think should be very easy, but so far I have not been able to get it to work.
What I want to do is map my root path to a Spring MVC Controller. With a normal Servlet
, I would just add a mapping for "/
" in my web.xml
, and it would pick it up quite well. But with Spring MVC, not so much.
I have tried many combinations, but none seem to work. I think the following one should work.
In web.xml
:
<servlet-mapping>
<servlet-name>myDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
In my contextConfigLocation
file:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true"/>
<property name="mappings">
<util:map>
<entry key="/" value-ref="rootController"/>
</util:map>
</property>
</bean>
<bean id="rootController" class="my.package.RootController">
Then obviously, there is the case of the controller itself. I have no clue how to map the method to the actual root path. My attempt is something like this:
public class RootController extends MultiActionController {
@RequestMapping("/")
public ModelAndView display(HttpServletRequest request, HttpServletResponse response) throws Exception {
final Map<String, Object> model = new HashMap<String, Object>();
model.put("someVariable", "Hello, MVC world.");
return new ModelAndView("rootPage", model);
}
}
So let's say my application runs on http://localhost:8080/app/
, I want that exact URL to execute the method display
. I do not want to type anything after /app/
. In fact, some things after /app/
are mapped to other controllers, and that all works fine (and they have to keep working).
What am I missing here? Why is this not simply working? If I use the same url-pattern
to map to a plain Servlet
instance, it works fine and I reach the doGet
method, but with Spring MVC I seem to missing some particular black magic to get this to work.
回答1:
Instead of mapping to /
you can declare a welcome page in your web.xml
file:
<welcome-file-list>
<welcome-file>welcome.htm</welcome-file>
</welcome-file-list>
so your /
path will be processed as /welcome.htm
and then if your controller is correctly mapped to /welcome.htm
it will process /
as if it was a /welcome.htm
request, without making changes to other configuration.
回答2:
I'd recommend getting rid of the SimpleUrlHandlerMapping and just doing the following:
@Controller
@RequestMapping("/")
public class RootController
{
@RequestMapping(method=RequestMethod.GET)
public ModelAndView display(...)
{
...
}
}
This should get the result you want. Also, add <mvc:annotation-driven/>
to your servlet context with a <context:component-scan base-package="some.package.path.to.controller" />
to have Spring wire up that controller.
Otherwise, you can probably map the URL with the SimpleUrlHandlerMapping as so:
<property name="mappings">
<value>
/*=rootController
</value>
<property>
If done this way, you can keep the bean defined for rootController.
回答3:
For Spring Webflow, the suggestion from Boris Treukhov to use web-file-list led to my discovering a hack that worked for weblogic. For example, if welcome is a flow.xml file, do the following: Change the url-pattern from \ to *.html in web.xml. Place a dummy(empty) file welcome.htm into the folder src\main\webapp\WEB-INF. In SimpleUrlHandlerMapping map the value /welcome.htm=flowController. Finally, complete the registry, flow-location path="welcome.xml". Refer to link: http://forum.spring.io/forum/spring-projects/web/50547-welcome-file-list-with-spring.
来源:https://stackoverflow.com/questions/12202302/map-root-url-in-spring-mvc