Spring MVC:如何为索引页创建默认控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5252065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC: how to create a default controller for index page?
提问by Hugo
I'm trying to do one of those standard spring mvc hello world applications but with the twist that I'd like to map the controller to the root. (for example: http://numberformat.wordpress.com/2009/09/02/hello-world-spring-mvc-with-annotations/) So the only real difference is that they map it to host\appname\something and I'd like to map it to host\appname.
我正在尝试做那些标准的 spring mvc hello world 应用程序之一,但是我想将控制器映射到根目录。(例如:http://numberformat.wordpress.com/2009/09/02/hello-world-spring-mvc-with-annotations/)所以唯一真正的区别是它们将它映射到 host\appname\something 和我想将它映射到主机\应用程序名称。
I placed my index.jsp in src\main\webapp\jsp and mapped it in the web.xml as the welcome file. I tried:
我将 index.jsp 放在 src\main\webapp\jsp 中,并将它映射到 web.xml 中作为欢迎文件。我试过:
@Controller("loginController")
public class LoginController{
@RequestMapping("/")
public String homepage2(ModelMap model, HttpServletRequest request, HttpServletResponse response){
System.out.println("blablabla2");
model.addAttribute("sigh", "lesigh");
return "index";
}
As my controller but I see nothing appear in the console of my tomcat. Does anyone know where I'm messing up?
作为我的控制器,但我在 tomcat 的控制台中看不到任何内容。有谁知道我在哪里搞砸了?
My web.xml:
我的 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- Index -->
<welcome-file-list>
<welcome-file>/jsp/index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>springweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springweb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
The mvc-dispatcher-servlet.xml:
mvc-dispatcher-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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="de.claude.test.*" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
I'm using Spring 3.0.5.release
我正在使用 Spring 3.0.5.release
Or is this not possible and do I need to put my index.jsp back in the root of the web-inf and put a redirect to somewhere inside my jsp so the controller picks it up?
或者这是不可能的,我是否需要将我的 index.jsp 放回 web-inf 的根目录并将重定向放在我的 jsp 中的某个位置以便控制器接收它?
回答by Mike M. Lin
I had the same problem, even after following Sinhue's setup, but I solved it.
即使按照 Sinhue 的设置,我也遇到了同样的问题,但我解决了。
The problem was that that something (Tomcat?) was forwarding from "/" to "/index.jsp" when I had the file index.jsp in my WebContent directory. When I removed that, the request did not get forwarded anymore.
问题是当我的 WebContent 目录中有文件 index.jsp 时,某些东西(Tomcat?)正在从“/”转发到“/index.jsp”。当我删除它时,请求不再被转发。
What I did to diagnose the problem was to make a catch-all request handler and printed the servlet path to the console. This showed me that even though the request I was making was for http://localhost/myapp/, the servlet path was being changed to "/index.html". I was expecting it to be "/".
我为诊断问题所做的工作是制作一个包罗万象的请求处理程序并将 servlet 路径打印到控制台。这表明即使我发出的请求是针对http://localhost/myapp/ 的,servlet 路径也被更改为“/index.html”。我期待它是“/”。
@RequestMapping("*")
public String hello(HttpServletRequest request) {
System.out.println(request.getServletPath());
return "hello";
}
So in summary, the steps you need to follow are:
因此,总而言之,您需要遵循的步骤是:
- In your servlet-mapping use
<url-pattern>/</url-pattern> - In your controller use
RequestMapping("/") - Get rid of welcome-file-list in web.xml
- Don't have any files sitting in WebContent that would be considered default pages(index.html, index.jsp, default.html, etc)
- 在您的 servlet 映射中使用
<url-pattern>/</url-pattern> - 在您的控制器中使用
RequestMapping("/") - 去掉 web.xml 中的欢迎文件列表
- WebContent 中没有任何文件会被视为默认页面(index.html、index.jsp、default.html 等)
Hope this helps.
希望这可以帮助。
回答by Emmanuel Ballerini
The redirect is one option. One thing you can try is to create a very simple index page that you place at the root of the WAR which does nothing else but redirecting to your controller like
重定向是一种选择。您可以尝试的一件事是创建一个非常简单的索引页面,您将其放置在 WAR 的根目录下,除了重定向到您的控制器之外什么都不做
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:redirect url="/welcome.html"/>
Then you map your controller with that URL with something like
然后你用那个 URL 映射你的控制器,比如
@Controller("loginController")
@RequestMapping(value = "/welcome.html")
public class LoginController{
...
}
Finally, in web.xml, to have your (new) index JSP accessible, declare
最后,在 web.xml 中,为了让您的(新)索引 JSP 可访问,声明
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
回答by Sathish Kumar Thiyagarajan
We can simply map a Controller method for the default view. For eg, we have a index.html as the default page.
我们可以简单地为默认视图映射一个 Controller 方法。例如,我们有一个 index.html 作为默认页面。
@RequestMapping(value = "/", method = GET)
public String index() {
return "index";
}
once done we can access the page with default application context.
完成后,我们可以使用默认应用程序上下文访问页面。
E.g http://localhost:8080/myapp
回答by sinuhepop
It works for me, but some differences:
它对我有用,但有一些区别:
- I have no welcome-file-list in web.xml
- I have no @RequestMapping at class level.
- And at method level, just @RequestMapping("/")
- 我在 web.xml 中没有欢迎文件列表
- 我在类级别没有@RequestMapping。
- 在方法级别,只需@RequestMapping("/")
I know these are no great differences, but I'm pretty sure (I'm not at work now) this is my configuration and it works with Spring MVC 3.0.5.
我知道这些没有太大区别,但我很确定(我现在不在工作)这是我的配置,它适用于 Spring MVC 3.0.5。
One more thing. You don't show your dispatcher configuration in web.xml, but maybe you have some preffix. It has to be something like this:
还有一件事。您没有在 web.xml 中显示您的调度程序配置,但也许您有一些前缀。它必须是这样的:
<servlet-mapping>
<servlet-name>myServletName</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
If this is not your case, you'll need an url-rewrite filter or try the redirect solution.
如果这不是您的情况,您将需要一个 url-rewrite 过滤器或尝试重定向解决方案。
EDIT:Answering your question, my view resolver configuration is a little different too:
编辑:回答你的问题,我的视图解析器配置也有点不同:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
回答by Roman Platonov
It can be solved in more simple way: in web.xml
可以用更简单的方式解决:在 web.xml 中
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
After that use any controllers that your want to process index.htm with @RequestMapping("index.htm"). Or just use index controller
之后,使用您想要通过@RequestMapping("index.htm") 处理 index.htm 的任何控制器。或者只使用索引控制器
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
<bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</bean>
回答by Shalin Patel
Just put one more entry in your spring xml file i.e.mvc-dispatcher-servlet.xml
只需在您的 spring xml 文件中再添加一项,即mvc-dispatcher-servlet.xml
<mvc:view-controller path="/" view-name="index"/>
After putting this to your xml put your default view or jsp file in your custom JSP folder as you have mentioned in mvc-dispatcher-servlet.xmlfile.
将其放入您的 xml 后,将您的默认视图或 jsp 文件放入您在mvc-dispatcher-servlet.xml文件中提到的自定义 JSP 文件夹中。
change indexwith your jsp name.
更改index为您的 jsp 名称。
回答by Daniel De León
One way to achieve it, is by map your welcome-file to your controller request path in the web.xmlfile:
实现它的一种方法是将您的欢迎文件映射到文件中的控制器请求路径web.xml:
[web.xml]
[web.xml]
<web-app ...
<!-- Index -->
<welcome-file-list>
<welcome-file>home</welcome-file>
</welcome-file-list>
</web-app>
[LoginController.java]
[登录控制器.java]
@Controller("loginController")
public class LoginController{
@RequestMapping("/home")
public String homepage2(ModelMap model, HttpServletRequest request, HttpServletResponse response){
System.out.println("blablabla2");
model.addAttribute("sigh", "lesigh");
return "index";
}
回答by bobmarksie
The solution I use in my SpringMVC webapps is to create a simple DefaultControllerclass like the following: -
我在 SpringMVC webapps 中使用的解决方案是创建一个简单的DefaultController类,如下所示:-
@Controller
public class DefaultController {
private final String redirect;
public DefaultController(String redirect) {
this.redirect = redirect;
}
@RequestMapping(value = "/")
public ModelAndView redirectToMainPage() {
return new ModelAndView("redirect:/" + redirect);
}
}
The redirect can be injected in using the following spring configuration: -
可以使用以下 spring 配置注入重定向:-
<bean class="com.adoreboard.farfisa.controller.DefaultController">
<constructor-arg name="redirect" value="${default.redirect:loginController}"/>
</bean>
The ${default.redirect:loginController}will default to loginControllerbut can be changed by inserting default.redirect=something_elseinto a spring properties file / setting an environment variable etc.
在${default.redirect:loginController}将默认loginController,但可以通过将被改变default.redirect=something_else成弹簧属性文件/设置的环境变量等。
As @Mike has mentioned above I have also: -
正如@Mike 上面提到的,我也有:-
- Got rid of
<welcome-file-list> ... </welcome-file-list>section in theweb.xmlfile. - Don't have any files sitting in WebContent that would be considered default pages (
index.html,index.jsp,default.html, etc)
- 删除
<welcome-file-list> ... </welcome-file-list>了web.xml文件中的部分。 - 不用坐在的WebContent将被视为默认页面的任何文件(
index.html,index.jsp,default.html等)
This solution lets Spring worry more about redirects which may or may not be what you like.
这个解决方案让 Spring 更加担心重定向,这些重定向可能是您喜欢的,也可能不是您喜欢的。

