spring tools suite默认的mvc项目web应用程序

时间:2014-08-26 15:18:24

标签: java spring spring-mvc web-applications uri

您好我正在尝试更改我的默认主页另一个但我无法做到,所以我尝试将页面从默认主页重定向到我想要的那个,但是我收到此错误 找不到带有URI的HTTP请求的映射:

  带有名称的DispatcherServlet中的

[/ myapp / WEB-INF / views / index.html]   'appServlet'

这是我的设置,我在使用spring工具套件创建项目时默认保留所有内容

这是我的servlet-contex.xml

<?xml version="1.0" encoding="UTF-8"?>
 <beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value="" />
</beans:bean>

<context:component-scan base-package="com.proj.myapp" />

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
 <web-app version="2.5" 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">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

这是我的聋人家庭档案

针对home.jsp

enter code here 
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page session="false" %>
      <html>
        <head>
          <title>Home</title>
        </head>
              <body>
               <h1>
                Hello world!  
                    <c:redirect url="/indice.html"/>
                </h1>
              </body>
       </html>

这是我想要重定向的页面

enter code here 
    <html>

           <body>
            <h1>
                INDICE
            </h1>
           </body>
    </html>

和我的控制器

        @Controller
        public class HomeController {


        @RequestMapping(value = "/")
        public String home() {


        return "home.jsp";
        }

        @RequestMapping(value = "/indice.html")
        public String indice() {


        return "indice.html";
        }
 }

我收到此错误

  

找不到包含URI的HTTP请求的映射:   [/myapp/WEB-INF/views/indice.html]在DispatcherServlet中的名称   'appServlet'

1 个答案:

答案 0 :(得分:0)

从代码后缀属性值为空,因此您需要在servlet-context.xml中将后缀类型传递给此属性。

你必须改变它

<beans:property name="suffix" value=".jsp" /> 

和您的indices.html到indices.jsp然后它将起作用。

但是如果你想用html文件工作,但.html文件是静态的,不需要servlet处理,那么使用映射会更有效,也更简单。这需要Spring 3.0.4 +。

例如:

<mvc:resources mapping="/static/**" location="/static/" />

将以/ static /开头的所有请求传递给webapp / static /目录。

所以把indices.html放在webapp / static /中并使用return&#34; indices.html&#34 ;;从你的方法,Spring应该找到视图。