简体   繁体   English

无法使用Spring MVC加载view.jsp

[英]Can't load view.jsp using Spring MVC

I'm new in Spring MVC and try to make an exercise, like in the example. 我是Spring MVC的新手,并尝试进行练习,如示例中所示。 I have index.jsp and view.jsp. 我有index.jsp和view.jsp。 Index.jsp is loaded well, but when I try load view.jsp, it teturns empty field and logger doesnt show me any mistake. Index.jsp加载得很好,但是当我尝试加载view.jsp时,它变成了空字段,并且记录器没有显示任何错误。 I use IntellijIDEA. 我使用IntellijIDEA。 Below all classes and files, let me know, if i have to give other info. 在所有类和文件下面,如果需要提供其他信息,请告诉我。 Thanks. 谢谢。

web.xml 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>

dispatcher-servlet.xml dispatcher-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>

Controller (System.out.println(shape.square()) shows me, that mapping do work) 控制器(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 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 view.jsp

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

Please check import 请检查进口

import org.springframework.web.portlet.ModelAndView;

I tried changing it to import org.springframework.web.servlet.ModelAndView; 我尝试将其更改为import org.springframework.web.servlet.ModelAndView; and it worked for me. 它为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM