2011-07-13 13 views
0

Dynamic Web Projectを使用してEclipse内でSpring MVC 3.0を使用してアプリケーションを構築しようとしています。私は最初のページに行くことができますが、そこから他のページに移動することはできません.404エラーが発生し、コントローラクラスにブレークポイントを打っていません。もし私に行方不明のものがあれば教えてください。ありがとう!Spring MVC 3.0 404エラー

applicationContext.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-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"> 

    <!-- Activates various annotations to be detected in bean classes --> 
    <context:annotation-config /> 

    <!-- 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 --> 
    <context:component-scan base-package="src" /> 

    <!-- Configures the annotation-driven Spring MVC Controller programming 
     model. Note that, with Spring 3.0, this tag works in Servlet MVC only! --> 
    <mvc:annotation-driven /> 

    <!-- Load Hibernate related configuration --> 
    <import resource="hibernate-context.xml" /> 

</beans> 

春-servlet.xmlコード:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 

    <bean 
     class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 

    <bean 
     class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 


    <!-- Declare a view resolver --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
      <property name="prefix" value="/WEB-INF/jsp"/> 
      <property name="suffix" value=".jsp"/> 
     </bean> 


</beans> 

のweb.xmlコード:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 

    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
      <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/applicationContext.xml 
    </param-value> 
    </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>/SpringHibernateExample/*</url-pattern> 
    </servlet-mapping> 

    <welcome-file-list> 
     <welcome-file>/jsp/personspage.jsp</welcome-file> 
    </welcome-file-list> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

</web-app> 

MainController.javaコード:

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<Person> 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"; 
    } 

} 

personspage.jspコード:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!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=UTF-8"> 
<title>People Page</title> 
</head> 
<body> 
<h1>Persons</h1> 

<c:url var="addUrl" value="/main/persons/add" /> 
<table style="border: 1px solid; width: 500px; text-align:center"> 
    <thead style="background:#fcf"> 
     <tr> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Money</th> 
      <th colspan="3"></th> 
     </tr> 
    </thead> 
    <tbody> 
    <c:forEach items="${persons}" var="person"> 
      <c:url var="editUrl" value="/persons/edit?id=${person.id}" /> 
      <c:url var="deleteUrl" value="/persons/delete?id=${person.id}" /> 
     <tr> 
      <td><c:out value="${person.firstName}" /></td> 
      <td><c:out value="${person.lastName}" /></td> 
      <td><c:out value="${person.money}" /></td> 
      <td><a href="${editUrl}">Edit</a></td> 
      <td><a href="${deleteUrl}">Delete</a></td> 
      <td><a href="${addUrl}">Add</a></td> 
     </tr> 
    </c:forEach> 
    </tbody> 
</table> 

<c:if test="${empty persons}"> 
    There are currently no persons in the list. <a href="${addUrl}">Add</a> a person. 
</c:if> 

</body> 
</html> 

答えて

0

おそらくこれはあなたの問題かもしれません:

<context:component-scan base-package="src" /> 

あなたは "基本パッケージ" のための入力あなたの基本パッケージ名になっています。たとえば、

<context:component-scan base-package="com.stackoverflow.project" /> 

ここで、 "com.stackoverflow.project"はコントローラで使用したパッケージ名です。

例:コントローラクラス:

package com.stackoverflow.project.controller 

@Controller 
public class DashboardController { ... 

お役に立てば幸いです。

+0

ありがとう、それは私のコントローラクラスに表示されているようにコントローラと呼ばれるsrcの下にフォルダがある場合、ベースパッケージは何でしょうか?それが "コントローラ"であるべきかどうか、もしそうなら、Springは他のクラスへの可視性を持つことができますか? – felicia

+0

はい "コントローラ"を入れてください。 – tshenolo

関連する問題