代码之家  ›  专栏  ›  技术社区  ›  Ritesh Puri

不调用带有JSP-Controller类的Spring

  •  0
  • Ritesh Puri  · 技术社区  · 6 年前

    我真的需要帮助。我正在练习用JSP开发一个基于springmvc的应用程序。我从基础开始,试着把 页面.jsp “内容,但总是打印” “内容。如果我删除“ 索引.html “然后我得到一个404错误。似乎没有扫描控制器类。我被困在这里,找不到任何解决办法。以下是我的代码:

    网站.xml

    <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">
      <display-name>Archetype Created Web Application</display-name>
    
        <!-- CONFIGURING FRONT CONTROLLER -->
        <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>/</url-pattern> 
        </servlet-mapping>
    
    </web-app>
    

    页面控制器.java

    package net.ritz.onlineshopping.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class PageController {
        
        @RequestMapping(value= {"/","/home","/index"})
        public ModelAndView index() {   
            ModelAndView mv = new ModelAndView("page");
            mv.addObject("greeting", "Welcome to Spring Web MVC");
            return mv;
        }
    }
    

    页面.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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>Online Shopping</title>
    </head>
    <body>
        ${greeting}
    </body>
    </html>
    

        <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns = "http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
       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-3.0.xsd">
       
       <mvc:annotation-driven  />
       <context:component-scan base-package="net.ritz.onlineshopping.controller"/>
       
       <bean id="viewResolver"
                  class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
                  <property name="prefix" value="/WEB-INF/views/"/>
                  <property name="suffix" value=".jsp"/>
            </bean>
       
       
       
       </beans>
       
    

    索引.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    This is Index
    </body>
    </html>
    

    在浏览器中输入此URL时:

    我得到以下输出,即index.html,但它应该映射到page.jsp: enter image description here

    以下是我的项目结构: enter image description here

    3 回复  |  直到 3 年前
        1
  •  1
  •   Ritesh Puri    6 年前

        2
  •  0
  •   Alien    6 年前

    您可以尝试以下解决方案之一。

    <welcome-file-list>index.html</welcome-file-list> 如果存在,则将其删除。

    或者

    或者

    创建一个不同的端点并检查控制器是否返回如下所示的jsp页面。

    @RequestMapping(value="/test")
        public ModelAndView test() {   
            ModelAndView mv = new ModelAndView("page");
            mv.addObject("greeting", "Welcome to Spring Web MVC");
            return mv;
        }
    
        3
  •  0
  •   Huy Nguyen    6 年前

    仔细检查你的地图,看看它是否有效。您可以调试或添加更多日志以确保:

        @RequestMapping(value= {"/","/home","/index"})
        public ModelAndView index() {   
            ModelAndView mv = new ModelAndView("page");
            mv.addObject("greeting", "Welcome to Spring Web MVC");
            return mv;
        }
    

    基本上一个没有的@Controller是,嗯,非常无用的,因为它只会占用内存。它不会绑定到传入的请求,它只是挂在应用程序上下文中。它只是一个和其他豆子一样的豆子,没有什么特别的处理(Spring的最新版本(但已弃用)注册了处理@Controller的DefaultAnnotationHandlerMapping(但已弃用)。

    Spring support for @Controller given by <context:component-scan /> vs <mvc:annotation-driven>

    <context:annotation-config/> <!-- is used to activate the annotation for beans -->
    <mvc:annotation-driven/>