无法使用 Spring MVC 加载 view.jsp

Posted

技术标签:

【中文标题】无法使用 Spring MVC 加载 view.jsp【英文标题】:Can't load view.jsp using Spring MVC 【发布时间】:2016-05-12 18:11:44 【问题描述】:

我是 Spring MVC 的新手,并尝试做一个练习,就像在示例中一样。我有 index.jsp 和 view.jsp。 Index.jsp 加载得很好,但是当我尝试加载 view.jsp 时,它返回空字段并且记录器没有显示任何错误。我使用 IntellijIDEA。在所有类和文件下方,如果我必须提供其他信息,请告诉我。谢谢。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.form</url-pattern>
</servlet-mapping>

调度程序-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-3.1.xsd
   http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
   ">

<context:component-scan base-package="controllers"/>

<bean id="vievResolver" class ="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="HttpGatewayInit" class ="controllers.HttpGatewayInit">
</bean>

<bean name="shape" class="shapes.Rectangle">
    <constructor-arg index="0" value ="12" />
    <constructor-arg index="1" value ="2" />
</bean>

<bean name="circle" class="shapes.Circle">
    <constructor-arg index="0" value ="8" />
</bean>

<bean name="rectanglepoint" class="shapes.RectanglePoint">
    <constructor-arg index="0" ref ="leftend" />
    <constructor-arg index="1" ref ="rightend" />
</bean>

<bean name="leftend" class="shapes.Point">
    <constructor-arg index="0" value ="4" />
    <constructor-arg index="1" value ="3" />
</bean>

<bean name="rightend" class="shapes.Point">
    <property name="x" value ="12" />
    <property name="y" value ="8" />
</bean>

控制器(System.out.println(shape.square()) 告诉我,映射确实有效)

package controllers;

import org.springframework.asm.Attribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
import shapes.Shape;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;


@Controller
public class MainController 

@Autowired
@Qualifier("shape")
private Shape shape;

public MainController()


@RequestMapping (value="/shape.form",method = RequestMethod.GET)
public ModelAndView main()
    /*class HttpGatewayInit implements WebApplicationInitializer 
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException 

        
    */

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("view");
    modelAndView.addObject("message", shape.square());
    System.out.println(shape.square());


    return modelAndView;



index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
 <head>
  <title>$Title$</title>
 </head>
 <body>
  <a href = "shape.form">Next page</a>
 </body>
</html>

view.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
 <head>
  <title>View</title>
 </head>
 <body>
  <span>$message</span>
 </body>
</html>

【问题讨论】:

我猜你的意思是 span 这里? $message。将其更改为 $message

$message

【参考方案1】:

请检查导入

import org.springframework.web.portlet.ModelAndView;

我尝试将其更改为 import org.springframework.web.servlet.ModelAndView; ,它对我有用。

【讨论】:

伙计,你是我最好的!它也对我有用。 IDE 向我推荐了 2 个导入变体,但我机械地选择了错误。 这是我在 *** 上的第一篇文章,我不知道如何标记,这是正确的答案并提高您的声誉)网站对我说,我必须有 15 个声誉才能投票。跨度>

以上是关于无法使用 Spring MVC 加载 view.jsp的主要内容,如果未能解决你的问题,请参考以下文章

Spring 5 MVC Angular 5 App投掷404错误

使用spring MVC重新加载弹出页面

spring静态资源无法访问

使用请求参数将javascript变量传递给spring MVC控制器

从 Thymeleaf 调用并加载 Spring MVC 控制器请求方法

spring mvc整合shiro无法访问控制器是啥问题