2013-04-17 34 views
1

的Spring MVC模板項目我下面教程http://krams915.blogspot.ie/2011/01/spring-mvc-3-hibernate-annotations.html在Spring工具套件

我有麻煩,當我輸入localhost:8080 /教程/ krams /主/人到地址欄。我不斷收到404錯誤。我的頂級軟件包是org.krams.tutorial,我已經將該項目的名稱設置爲教程。我刪除了Home Controller.java和home.jsp,我不確定這是否會產生影響Spring MVC模板的目錄結構與Krams不同,當我下載Krams項目並在STS中運行它時,它工作正常,但我想從頭開始自己做。希望有人能幫助我!它非常令人沮喪! 這是我的代碼。

根的context.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="org.krams.tutorial" /> 

    <!-- 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的context.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     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"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <context:component-scan base-package="org.krams.tutorial" /> 



</beans:beans> 

休眠-context.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:p="http://www.springframework.org/schema/p" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd 
      "> 

    <context:property-placeholder location="/WEB-INF/spring/spring.properties" /> 

    <!-- Enable annotation style of managing transactions --> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions --> 
    <!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->       
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html --> 
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html --> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
       p:dataSource-ref="dataSource" 
       p:configLocation="${hibernate.config}" 
       p:packagesToScan="org.krams.tutorial"/> 

    <!-- Declare a datasource that has pooling capabilities--> 
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
       destroy-method="close" 
       p:driverClass="${app.jdbc.driverClassName}" 
       p:jdbcUrl="${app.jdbc.url}" 
       p:user="${app.jdbc.username}" 
       p:password="${app.jdbc.password}" 
       p:acquireIncrement="5" 
       p:idleConnectionTestPeriod="60" 
       p:maxPoolSize="100" 
       p:maxStatements="50" 
       p:minPoolSize="10" /> 

    <!-- Declare a transaction manager--> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
       p:sessionFactory-ref="sessionFactory" /> 

</beans> 

個的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
    <!-- We're using MySQL database so the dialect needs to MySQL as well--> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 
    <!-- Enable this to see the SQL statements in the logs--> 
    <property name="show_sql">false</property> 
    <!-- This will drop our existing database and re-create a new one. 
      Existing data will be deleted! --> 
    <property name="hbm2ddl.auto">create</property> 
    </session-factory> 
</hibernate-configuration> 

spring.properties

# database properties 
app.jdbc.driverClassName=com.mysql.jdbc.Driver 
app.jdbc.url=jdbc:mysql://localhost/mydatabase 
app.jdbc.username=root 
app.jdbc.password=nbuser 

#hibernate properties 
hibernate.config=/WEB-INF/spring/hibernate.cfg.xml 

MainController.java

package org.krams.tutorial.controller; 

import java.util.List; 

import javax.annotation.Resource; 

import org.apache.log4j.Logger; 
import org.krams.tutorial.domain.Person; 
import org.krams.tutorial.service.PersonService; 
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; 


/** 
* Handles and retrieves person request 
*/ 
@Controller 
@RequestMapping("krams/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"; 
    } 

} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" 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_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 
+0

有兩個問題記​​錄器是否打印出任何內容,表明您已經選擇了該方法,並且可以發佈您的web.xml文件? –

+0

我在http://www.jamesanthony527.com上發佈了記錄器打印 –

回答

0

尋找你的日誌,我可以看到你出現以下情況例外:拋出java.lang.ClassNotFoundException:

所致實現org.hibernate.cache.CacheProvider

快速看看谷歌導致在SO線程here,說明問題是與您可能與春季結合使用的休眠版本。使用建議的Factory編輯您的hibernate-context.xml可能會解決您的問題。

相關問題