【问题标题】:Spring with JSP - Controller class is not called带有 JSP 的 Spring - 不调用控制器类
【发布时间】:2019-02-26 07:34:06
【问题描述】:

我真的需要这方面的帮助。我正在练习使用 JSP 开发基于 Spring MVC 的应用程序。我从基本开始并尝试打印“page.jsp”内容,但它总是打印“index.html”内容。如果我删除“index.html”,则会收到 404 错误。似乎 Controller 类没有被扫描。我被困在这里,我没有找到任何解决方案。以下是我的代码:

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

PageController.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;
    }
}

page.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>

dispatcher-servlet.xml

    <?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>
   

index.html

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

当我在浏览器中输入这个网址时:http://localhost:8080/onlineshopping/

我得到以下输出,它是 index.html,但它应该映射到 page.jsp:

以下是我的项目结构:

【问题讨论】:

  • 您应该有一个切片 XML 配置文件,该文件将文件 page.jsp 与模型名称页面映射。检查这是否正确。
  • 对不起,我没听懂。

标签: java spring spring-mvc jsp servlets


【解决方案1】:

经过大量研究,我找到了这个烦人问题的答案。因此,右键单击项目,然后单击属性。选择“部署程序集”,然后单击“添加”按钮。选择“Java Build Path Entries”,单击“下一步”,然后选择“Maven Dependencies”并单击“确定”。右键单击 Servers 部分下的 Tomcat Server,然后选择“Clean”。再次右键单击 Tomcat 服务器并选择“发布”。我在 Tomcat 服务器上运行我的项目并解决了 404 not found 错误。我能够打印 page.jsp 内容!!

【讨论】:

    【解决方案2】:

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

    检查您的 xml 配置是否存在任何 &lt;welcome-file-list&gt;index.html&lt;/welcome-file-list&gt;,如果存在则将其删除。

    尝试删除index.html文件,查看控制器是否返回jsp页面。

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

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

    【讨论】:

    • 首先,我没有定义 标签。其次,如果我删除 index.html 文件,我会收到“404 Not Found”错误的描述:“源服务器没有找到目标资源的当前表示或不愿意透露存在的表示。”你能帮我解决第三点吗?
    • 我包含了这段代码并运行了“localhost:8080/onlineshopping/test”,但仍然遇到相同的 404 not found 错误。
    • 你能用测试打控制器吗?
    • 不,我不是。我怀疑在 dispatcher-servlet.xml 中定义的 是否正常工作。如果我输入 url “localhost:8080/onlineshopping”,那么它应该显示 page.jsp 内容,但如果定义了这个文件,它会映射到 index.html,否则会出现 404 未找到错误。
    【解决方案3】:

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

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

    基本上没有@Controller 是非常无用的,因为它只会占用内存。它不会绑定到传入的请求,它只是挂在应用程序上下文中。它只是与所有其他 bean 一样的另一种 bean,并没有对它做任何特别的事情。 (最近但不推荐使用的 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/>
    

    【讨论】:

    • 看来项目默认需要先映射到index.html。如果我没有这个文件,那么我会收到一条错误消息,否则即使 URL 应根据您显示的代码映射到 page.jsp,也会显示 index.html 内容。
    • 我不确定所有设置是否正确,所以我对此表示怀疑。我建议您可以尝试映射其他 url,如“/test”以进行测试,并返回一个视图页面,看看它是如何工作的。
    • 我确实尝试映射到不同的端点(/test),但仍然遇到同样的问题。
    • 是的,似乎组件扫描不适用于@Controller,您应该添加更多 并重新检查docs.spring.io/spring/docs/current/spring-framework-reference/…
    • 我已经按照其他地方建议的解决方案尝试了这个,但它对我不起作用,但我同意你的组件扫描无法扫描我的控制器类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    相关资源
    最近更新 更多