2013-04-21 54 views
0

所以我知道有幾十個類似這個帖子,但不幸的是沒有人幫助我。我只是試圖讓一個演示Spring MVC項目啓動並運行。我正在嘗試運行由Heroku提供的模板項目(https://devcenter.heroku.com/articles/getting-started-with-heroku-eclipse)。我嘗試過很多設置組合,無濟於事。以下是默認設置:在開始時沒有找到Spring MVC資源

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5"> 
<display-name>Spring-Hibernate-Template</display-name> 

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

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:applicationContext.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>/people/*</url-pattern> 
</servlet-mapping> 
</web-app> 

的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:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

<context:annotation-config /> 
<context:component-scan base-package="com.example" /> 

<mvc:annotation-driven/> 

<bean id="jspViewResolver" 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> 

<tx:annotation-driven /> 
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    <property name="dataSource" ref="dataSource"/> 

</bean> 

<beans profile="default"> 
    <jdbc:embedded-database id="dataSource"/>   
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 
     </property> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop> 
       <prop key="hibernate.hbm2ddl.auto">create</prop> 
      </props> 
     </property> 
    </bean> 
</beans> 

<beans profile="prod"> 
    <bean class="java.net.URI" id="dbUrl"> 
     <constructor-arg value="#{systemEnvironment['DATABASE_URL']}"/> 
    </bean> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
     <property name="url" value="#{ 'jdbc:postgresql://' + @dbUrl.getHost() + ':' + @dbUrl.getPort() + @dbUrl.getPath() }"/> 
     <property name="username" value="#{ @dbUrl.getUserInfo().split(':')[0] }"/> 
     <property name="password" value="#{ @dbUrl.getUserInfo().split(':')[1] }"/> 
    </bean> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 
     </property> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <!-- change this to 'verify' before running as a production app --> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
      </props> 
     </property> 
    </bean> 
</beans> 

</beans> 

PersonController.java:

@Controller 
public class PersonController { 

    @Autowired 
    private PersonService personService; 

    @RequestMapping("/") 
    public String listPeople(Map<String, Object> map) { 

     map.put("person", new Person()); 
     map.put("peopleList", personService.listPeople()); 

     return "people"; 
    } 

    @RequestMapping(value = "/add", method = RequestMethod.POST) 
    public String addPerson(@ModelAttribute("person") Person person, BindingResult result) { 

     personService.addPerson(person); 

     return "redirect:/people/"; 
    } 

    @RequestMapping("/delete/{personId}") 
    public String deletePerson(@PathVariable("personId") Integer personId) { 

     personService.removePerson(personId); 

     return "redirect:/people/"; 
    } 
} 

而且我有一個「人。 jsp'webapp/WEB-INF/jsp/people/jsp上的文件

Tomcat服務器server.xml中我context元素的樣子:

<Context docBase="facultypublicationsdb" path="/facultypublicationsdb" reloadable="true" source="org.eclipse.jst.jee.server:facultypublicationsdb"/></Host> 

每次我在(http://localhost:8080/facultypublicationsdb/)在Tomcat運行它,我得到如下:

HTTP Status 404 - /facultypublicationsdb/ 

type Status report 

message /facultypublicationsdb/ 

description The requested resource (/facultypublicationsdb/) is not available. 

Apache Tomcat/7.0.21 

我運行這個在Ubuntu上通過eclipse。我注意到沒有.war被複制到/usr/share/tomcat7/webapps目錄。這是否應該發生?

任何想法?

+0

首先,我注意到在applicationContext.xml中,在xml文件的末尾有兩個結束bean標記。 – blackpanther 2013-04-21 20:40:28

+0

你在使用Maven嗎? – 2013-04-21 20:42:59

+0

@blackpanther還有兩個開放的bean標籤。 – 2013-04-21 20:51:36

回答

2

試着檢查兩件事情。

首先更改您的web.xml文件以將調度程序servlet映射到/。這會導致調度程序servlet在找不到任何其他映射時被使用,而不是每個請求。如果您擁有諸如CSS和Javascript之類的資源,這一點很重要。接下來,如果使用Eclipse,請打開您的項目屬性(單擊項目中的項目,瀏覽器Alt + Enter)。然後轉到部署程序集。確保您的所有項目資源都包含在這裏,尤其是任何Maven依賴項。如果您注意到缺少相關性,請單擊添加按鈕並選擇它們。

您可能還想確保您的控制器正在通過組件掃描進行拾取。確保您的控制器位於com.example包裝中。

+0

控制器是正確的包裝。依賴項被添加到部署程序集。我改變了url模式爲/但不幸的是沒有改變。 – 2013-04-21 21:00:47

+0

事實上,事實證明,依賴關係沒有添加到程序集中。感謝您的建議。將它們添加到程序集後,我繼續收到更多的錯誤,但下載m2eclipse錯誤清除了這些錯誤。 – 2013-04-21 22:23:59

+0

@JeremyG我很高興你的工作。幹得好! – 2013-04-21 22:31:26

0

我認爲你需要在listPeople方法中指定method = RequestMethod.GET。可能有許多小事情可能導致404 error。你可以上傳這個代碼到GitHub上,我會研究它。