【问题标题】:spring tools suite defaul mvc project web applicationspring 工具套件默认 mvc 项目 web 应用程序
【发布时间】:2014-10-19 23:52:51
【问题描述】:

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

[/myapp/WEB-INF/views/index.html] DispatcherServlet 中的名称 '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 请求的映射: DispatcherServlet 中的 [/myapp/WEB-INF/views/indice.html] 名称 'appServlet'

【问题讨论】:

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


    【解决方案1】:

    由于您的代码后缀属性值为空白,因此您需要在 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 "indices.html";根据您的方法,Spring 应该会找到视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-06
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多