Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC 3.0 404 error
    primarykey
    data
    text
    <p>I'm trying to build an application using Spring MVC 3.0 within Eclipse using the Dynamic Web Project. I'm able to get to the initial page, but I can not navigate to any other pages from there w/out getting a 404 error and I'm not hitting any of my breakpoints in the controller class. Please let me know if there's something I'm missing. Thanks!</p> <p>applicationContext.xml Code:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt; &lt;!-- Activates various annotations to be detected in bean classes --&gt; &lt;context:annotation-config /&gt; &lt;!-- Scans the classpath for annotated components that will be auto-registered as Spring beans. For example @Controller and @Service. Make sure to set the correct base-package --&gt; &lt;context:component-scan base-package="src" /&gt; &lt;!-- Configures the annotation-driven Spring MVC Controller programming model. Note that, with Spring 3.0, this tag works in Servlet MVC only! --&gt; &lt;mvc:annotation-driven /&gt; &lt;!-- Load Hibernate related configuration --&gt; &lt;import resource="hibernate-context.xml" /&gt; &lt;/beans&gt; </code></pre> <p>spring-servlet.xml Code:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xsi:schemaLocation=" http://www.springframework.org/schema/beans ... xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt; &lt;!-- Declare a view resolver --&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt; &lt;property name="prefix" value="/WEB-INF/jsp"/&gt; &lt;property name="suffix" value=".jsp"/&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>web.xml Code:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt; &lt;servlet&gt; &lt;servlet-name&gt;spring&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/applicationContext.xml &lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;spring&lt;/servlet-name&gt; &lt;url-pattern&gt;/SpringHibernateExample/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;/jsp/personspage.jsp&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;/web-app&gt; </code></pre> <p>MainController.java Code:</p> <pre><code>package controller; import java.util.List; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import service.PersonService; import domain.Person; /** * Handles and retrieves person request */ @Controller @RequestMapping("/main") public class MainController { protected static Logger logger = Logger.getLogger("controller"); @Resource(name = "personService") private PersonService personService; /** * Handles and retrieves all persons and show it in a JSP page * * @return the name of the JSP page */ @RequestMapping(value = "/persons", method = RequestMethod.GET) public String getPersons(Model model) { logger.debug("Received request to show all persons"); // Retrieve all persons by delegating the call to PersonService List&lt;Person&gt; persons = personService.getAll(); // Attach persons to the Model model.addAttribute("persons", persons); // This will resolve to /WEB-INF/jsp/personspage.jsp return "personspage"; } /** * Retrieves the add page * * @return the name of the JSP page */ @RequestMapping(value = "/persons/add", method = RequestMethod.GET) public String getAdd(Model model) { logger.debug("Received request to show add page"); // Create new Person and add to model // This is the formBackingOBject model.addAttribute("personAttribute", new Person()); // This will resolve to /WEB-INF/jsp/addpage.jsp return "addpage"; } /** * Adds a new person by delegating the processing to PersonService. Displays * a confirmation JSP page * * @return the name of the JSP page */ @RequestMapping(value = "/persons/add", method = RequestMethod.POST) public String add(@ModelAttribute("personAttribute") Person person) { logger.debug("Received request to add new person"); // The "personAttribute" model has been passed to the controller from // the JSP // We use the name "personAttribute" because the JSP uses that name // Call PersonService to do the actual adding personService.add(person); // This will resolve to /WEB-INF/jsp/addedpage.jsp return "addedpage"; } /** * Deletes an existing person by delegating the processing to PersonService. * Displays a confirmation JSP page * * @return the name of the JSP page */ @RequestMapping(value = "/persons/delete", method = RequestMethod.GET) public String delete( @RequestParam(value = "id", required = true) Integer id, Model model) { logger.debug("Received request to delete existing person"); // Call PersonService to do the actual deleting personService.delete(id); // Add id reference to Model model.addAttribute("id", id); // This will resolve to /WEB-INF/jsp/deletedpage.jsp return "deletedpage"; } /** * Retrieves the edit page * * @return the name of the JSP page */ @RequestMapping(value = "/persons/edit", method = RequestMethod.GET) public String getEdit( @RequestParam(value = "id", required = true) Integer id, Model model) { logger.debug("Received request to show edit page"); // Retrieve existing Person and add to model // This is the formBackingOBject model.addAttribute("personAttribute", personService.get(id)); // This will resolve to /WEB-INF/jsp/editpage.jsp return "editpage"; } /** * Edits an existing person by delegating the processing to PersonService. * Displays a confirmation JSP page * * @return the name of the JSP page */ @RequestMapping(value = "/persons/edit", method = RequestMethod.POST) public String saveEdit(@ModelAttribute("personAttribute") Person person, @RequestParam(value = "id", required = true) Integer id, Model model) { logger.debug("Received request to update person"); // The "personAttribute" model has been passed to the controller from // the JSP // We use the name "personAttribute" because the JSP uses that name // We manually assign the id because we disabled it in the JSP page // When a field is disabled it will not be included in the // ModelAttribute person.setId(id); // Delegate to PersonService for editing personService.edit(person); // Add id reference to Model model.addAttribute("id", id); // This will resolve to /WEB-INF/jsp/editedpage.jsp return "editedpage"; } } </code></pre> <p>personspage.jsp Code:</p> <pre><code>&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt; &lt;%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;title&gt;People Page&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Persons&lt;/h1&gt; &lt;c:url var="addUrl" value="/main/persons/add" /&gt; &lt;table style="border: 1px solid; width: 500px; text-align:center"&gt; &lt;thead style="background:#fcf"&gt; &lt;tr&gt; &lt;th&gt;First Name&lt;/th&gt; &lt;th&gt;Last Name&lt;/th&gt; &lt;th&gt;Money&lt;/th&gt; &lt;th colspan="3"&gt;&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;c:forEach items="${persons}" var="person"&gt; &lt;c:url var="editUrl" value="/persons/edit?id=${person.id}" /&gt; &lt;c:url var="deleteUrl" value="/persons/delete?id=${person.id}" /&gt; &lt;tr&gt; &lt;td&gt;&lt;c:out value="${person.firstName}" /&gt;&lt;/td&gt; &lt;td&gt;&lt;c:out value="${person.lastName}" /&gt;&lt;/td&gt; &lt;td&gt;&lt;c:out value="${person.money}" /&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="${editUrl}"&gt;Edit&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="${deleteUrl}"&gt;Delete&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="${addUrl}"&gt;Add&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/c:forEach&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;c:if test="${empty persons}"&gt; There are currently no persons in the list. &lt;a href="${addUrl}"&gt;Add&lt;/a&gt; a person. &lt;/c:if&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload