【发布时间】:2015-01-11 07:36:41
【问题描述】:
*朋友们,大家好!无论我尝试什么,我仍然收到 404 错误消息,我真的不知道该怎么办.. 我试图在 2 天内修复它,但仍然没有希望: 当我单击 index.jsp 上的链接,获取 view.jsp 页面时,我得到: HTTP 状态 404 - /WEB-INF/circle.jsp
IDE:Intellij Idea 13
文件结构: 您可以通过此链接在此处查看我的文件结构: http://dl1.joxi.net/drive/0007/2131/485459/150110/cc3837ee1a.jpg
错误图片: 您可以通过此链接在此处查看我的错误 img: http://dl2.joxi.net/drive/0007/2131/485459/150110/0c70f50d3c.jpg
MainController.java
@Controller
public class MainController {
@Autowired
@Qualifier("rectanglePoint")
private Shape rectanglePoint;
@Autowired
@Qualifier("rectangle")
private Shape rectangle;
@Autowired
@Qualifier("circle")
private Shape circle;
public MainController(){
}
@RequestMapping(value = "/rectanglepoint", method = RequestMethod.GET)
public ModelAndView rectanglePoint(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", rectanglePoint.square());
modelAndView.setViewName("view");
return modelAndView;
}
@RequestMapping(value = "/rectangle", method = RequestMethod.GET)
public ModelAndView rectangle(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", rectangle.square());
modelAndView.setViewName("view");
return modelAndView;
}
@RequestMapping(value = "/circle", method = RequestMethod.GET)
public ModelAndView circle(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", circle.square());
modelAndView.setViewName("view");
return modelAndView;
}
}
springframeworkmvc-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.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="viewResolver" 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="rectanglePoint" class="classes.RectanglePoint">
<constructor-arg index="0" ref="pointleft"/>
<constructor-arg index="1" ref="pointright"/>
</bean>
<bean id="circle" class="classes.Circle">
<constructor-arg index="0" value="5"/>
</bean>
<bean id="rectangle" class="classes.Rectangle ">
<constructor-arg index="0" value="2"/>
<constructor-arg index="1" value="5"/>
</bean>
<bean id="pointleft" class="classes.Point">
<constructor-arg name="x" value="1"/>
<constructor-arg name="y" value="2"/>
</bean>
<bean id="pointright" class="classes.Point">
<property name="x" value="2"/>
<property name="y" value="1"/>
</bean>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>springframework</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springframeworkmvc-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springframeworkmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springframeworkmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
view.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
${message}
</body>
</html>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head></head>
<body>
<a href="/circle.do">link</a>
</body>
</html>
【问题讨论】:
-
在您发布的图片中,我没有看到任何名为 circle.jsp 的文件
-
在您的 index.jsp 页面中尝试将 /circle.do 更改为 /view.do,如果可行,请告诉我,我会发布答案:D
-
不,它没有帮助。回答你的第一个问题:据我所知,我不需要直接创建 circle.jsp - 它将通过 Spring MVC 自动创建(在我的情况下正是 MainController.java),对吗?
-
是的,你是对的,我没有看到你的控制器中设置视图名称的最后几行 :) 它不会由 spring 创建,spring 将映射 url 并转到控制器的适当方法在那里你设置视图名称(“视图”),然后视图解析器将解析到适当的视图。
-
当您尝试在 circle.do 上导航时,浏览器中出现的确切网址是什么?
标签: java spring-mvc http-status-code-404 autowired modelattribute