Spring MVC:如何为索引页面创建默认控制器? [英] Spring MVC: how to create a default controller for index page?

查看:114
本文介绍了Spring MVC:如何为索引页面创建默认控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个标准的spring mvc hello world应用程序,但是我想将控制器映射到根. (例如: http://numberformat. wordpress.com/2009/09/02/hello-world-spring-mvc-with-annotations/) 因此,唯一真正的区别是他们将其映射到host \ appname \ something,而我想将其映射到host \ appname.

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.

我将index.jsp放置在src \ main \ webapp \ jsp中,并将其映射为web.xml中的欢迎文件. 我试过了:

I placed my index.jsp in src\main\webapp\jsp and mapped it in the web.xml as the welcome file. I tried:

@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";
  }

作为我的控制器,但是我在tomcat的控制台中什么也没看到. 有人知道我在搞砸吗?

As my controller but I see nothing appear in the console of my tomcat. Does anyone know where I'm messing up?

我的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>

mvc-dispatcher-servlet.xml:

The 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>

我正在使用Spring 3.0.5.release

I'm using Spring 3.0.5.release

或者这是不可能的,我是否需要将index.jsp放回web-inf的根目录并重定向到jsp内的某个位置,以便控制器将其拾取?

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?

推荐答案

即使遵循了Sinhue的设置,我也遇到了同样的问题,但是我解决了它.

I had the same problem, even after following Sinhue's setup, but I solved it.

问题是当WebContent目录中有文件index.jsp时,某些东西(Tomcat?)从"/"转发到"/index.jsp".当我删除该请求后,该请求不再转发.

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.

我诊断问题的方法是创建一个通用请求处理程序,并将servlet路径打印到控制台.这表明即使我发出的请求是针对 http://localhost/myapp/的,该servlet路径也已更改为"/index.html".我以为它是"/".

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 "/".

@RequestMapping("*")
public String hello(HttpServletRequest request) {
    System.out.println(request.getServletPath());
    return "hello";
}

因此,总而言之,您需要遵循的步骤是:

So in summary, the steps you need to follow are:

  1. 在servlet映射中使用<url-pattern>/</url-pattern>
  2. 在控制器中使用RequestMapping("/")
  3. 摆脱web.xml中的welcome-file-list
  4. WebContent中没有任何文件被视为默认页面(index.html,index.jsp,default.html等)
  1. In your servlet-mapping use <url-pattern>/</url-pattern>
  2. In your controller use RequestMapping("/")
  3. Get rid of welcome-file-list in web.xml
  4. Don't have any files sitting in WebContent that would be considered default pages (index.html, index.jsp, default.html, etc)

希望这会有所帮助.

这篇关于Spring MVC:如何为索引页面创建默认控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆