首页 > 解决方案 > 如何从控制器传递视图?

问题描述

我是 Spring MVC 的新手。我正在尝试使用多个视图。当我使用单个视图和单个控制器时,它可以完美运行。但是当我尝试从另一个视图打开一个视图时,我收到 HTTP 404 错误(未找到请求的资源)

项目目录结构

如图所示,我将 index.jsp 作为我的主页视图。从这个页面,我需要浏览另一个页面(viewpage1.jsp),它位于 WEB-INF/jsp 目录中。当我这样做时,我得到 HTTP 404 not found 错误。

HelloController.java

package com.springwebproject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController
{
    @RequestMapping("/")
    public String home()
    {
        return "index";
    }
    @RequestMapping("/hello")
    public String display()
    {
       return "viewpage1";
    }
}

index.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view>
<h1>Home Page</h1>
<a href="viewpage1">Go to Next View</a>
</f:view>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringWebProject</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
   <servlet>
        <servlet-name>SpringWebProject</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringWebProject</servlet-name>
        <url-pattern>/viewpage1.jsp</url-pattern>
    </servlet-mapping>
</web-app>

spring-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:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="  
        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  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  
    <!-- Provide support for component scanning -->  
    <context:component-scan base-package="com.springwebproject" />  
  
    <!--Provide support for conversion, formatting and validation -->  
    <mvc:annotation-driven/>  
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
          
     </bean>  
</beans>  

viewpage1.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view>
<h1>First View Page after Home page</h1>
</f:view>
</body>
</html>

标签: spring

解决方案


由于您已将映射指定为“/hello”以查看 viewpage1,因此您必须像这样更改您的 href:

<a href="/hello">Go to Next View</a>

让我知道,如果这有帮助。


推荐阅读