简体   繁体   中英

Spring MVC @Autowired and @ModelAttribute HTTP Status 404 - /WEB-INF/circle.jsp

*Good day to you all, friends! Whatever I try, I still get 404 error message, and I truly have no idea what to do.. I was trying to fix it during the 2 days, and still hopeless: When I click on the link on the index.jsp, insted of getting view.jsp page, I get: HTTP Status 404 - /WEB-INF/circle.jsp

IDE: Intellij Idea 13

file structure: You can view my file structure here via this link: http://dl1.joxi.net/drive/0007/2131/485459/150110/cc3837ee1a.jpg

error img: You can vie my error img here via this link: 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>

Thank you all for your support. I, by accedent, have found the misstake:

in MainController.java I've imported by IDEA suggest org.springframework.web.portlet.ModelAndView; insted of import org.springframework.web.servlet.ModelAndView; Now it works fine. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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