java - spring 工具套件默认 mvc 项目 Web 应用程序

标签 java spring spring-mvc web-applications uri

嗨,我正在尝试将默认主页更改为另一个页面,但我无法做到这一点,因此我尝试将页面从默认主页重定向到我想要的页面,但出现此错误 未找到带有 URI 的 HTTP 请求的映射:

[/myapp/WEB-INF/views/index.html] in DispatcherServlet with name '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

        @Controller
        public class HomeController {


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


        return "home.jsp";
        }

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


        return "indice.html";
        }
 }

我收到这个错误

No mapping found for HTTP request with URI: [/myapp/WEB-INF/views/indice.html] in DispatcherServlet with name 'appServlet'

最佳答案

由于您的代码后缀属性值为空,因此您需要将后缀类型传递给 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 应该找到 View 。

关于java - spring 工具套件默认 mvc 项目 Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25509483/

相关文章:

java - 动画静态属性?

java - 定义一个具有强制参数和可选参数的选项

java - 使用 Spring 工厂方法连接 ExecutorService

java - Spring AccessDeniedException 中的自定义消息

java - 评论框的边界

java - 使用 lotus.domino 在 Notes 文档中插入图像/文件

java - Autowired 在 Spring Boot 中返回 null

java - 最佳实践 : Creation of SAX parser for XMLReader

java - Lucene 的 Spring Bean 配置

spring-boot - 微服务究竟是如何实现的?