Java 未调用带有JSP-Controller类的Spring
我真的需要帮助。我正在练习用JSP开发一个基于SpringMVC的应用程序。我从基础开始,尝试打印“page.jsp”内容,但它总是打印“index.html”内容。如果删除“index.html”,则会出现404错误。控制器类似乎没有被扫描。我被困在这里,找不到任何解决办法。以下是我的代码: web.xmlJava 未调用带有JSP-Controller类的Spring,java,spring,spring-mvc,jsp,servlets,Java,Spring,Spring Mvc,Jsp,Servlets,我真的需要帮助。我正在练习用JSP开发一个基于SpringMVC的应用程序。我从基础开始,尝试打印“page.jsp”内容,但它总是打印“index.html”内容。如果删除“index.html”,则会出现404错误。控制器类似乎没有被扫描。我被困在这里,找不到任何解决办法。以下是我的代码: web.xml <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http
<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>
<?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>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
This is Index
</body>
</html>
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
<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>
<?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>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
This is Index
</body>
</html>
index.html
<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>
<?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>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
This is Index
</body>
</html>
在此处插入标题
这是索引
在浏览器中输入此URL时:http://localhost:8080/onlineshopping/
我得到以下输出,即index.html,但它应该映射到page.jsp:
以下是我的项目结构:
仔细检查您的映射,看看它是否有效。您可以调试或添加更多日志,以确保:
@RequestMapping(value= {"/","/home","/index"})
public ModelAndView index() {
ModelAndView mv = new ModelAndView("page");
mv.addObject("greeting", "Welcome to Spring Web MVC");
return mv;
}
基本上,没有@Controller的@Controller是非常无用的,因为它只会占用内存。它不会绑定到传入的请求,只是挂在应用程序上下文中。它只是一个和所有其他豆子一样的豆子,没有对它做任何特殊的处理。(Spring的最新但已弃用的版本注册了处理@Controller的DefaultAnnotationHandlerMapping,但已弃用)
尝试在设置中添加以下配置
<context:annotation-config/> <!-- is used to activate the annotation for beans -->
<mvc:annotation-driven/>
您可以尝试以下解决方案之一 检查xml配置是否存在任何
index.html
,如果存在,则删除该配置
或
尝试删除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;
}
在做了大量的研究之后,我找到了这个恼人问题的答案。因此,右键单击项目,然后单击属性。选择“部署程序集”,然后单击“添加”按钮。选择“Java构建路径条目”,单击Next,然后选择“Maven依赖项”并单击OK。右键单击Servers部分下的Tomcat服务器并选择“Clean”。再次右键单击Tomcat服务器并选择“发布”。我在Tomcat服务器上运行了我的项目,404NotFound错误得到了解决。我能够打印page.jsp内容 您应该有一个tiles XML配置文件,它将文件page.jsp映射到模型名称页面。检查这是否正确。很抱歉,我不明白。请看一下本教程。首先,我没有定义标记。其次,如果我删除index.html文件,则会出现“404 Not Found”(404 Not Found)错误,错误描述为:“源服务器未找到目标资源的当前表示形式,或者不愿意透露该表示形式的存在。”您能帮我解决第三点吗?我包含了此代码并运行了“”但仍然有相同的404未找到错误。你能使用测试点击控制器吗?不,我不能。我怀疑dispatcher-servlet.xml中定义的if是否正常工作。如果我输入url“”,那么它应该显示page.jsp内容,但是如果该文件被定义为404未找到错误,那么它将映射到index.html。默认情况下,项目需要首先映射到index.html。如果我没有此文件,则会收到一条错误消息,否则会显示index.html内容,即使URL应该按照您显示的代码映射到page.jsp。我不确定所有设置是否正确,因此我对此表示怀疑。我建议您可以尝试映射其他url,如“/test”,以进行测试,并返回一个查看页面,看看它是如何工作的。我确实尝试映射到不同的端点(/test),但仍然遇到相同的问题。是的,组件扫描似乎不适用于@Controller,您应该添加更多内容并重新检查,我已经按照建议在其他地方尝试了此解决方案,但它对我不起作用,但我同意您关于组件扫描的观点,即无法扫描我的控制器类。