2011-10-11 29 views
0

我是Spring MVC和Spring/Hibernate的新手。當我嘗試使用STS運行Spring MVC時,我得到請求的資源()不可用

我想運行我在網上找到的例子,試圖做到這一點,但無法成功運行它。

我的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"> 

<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

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

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

</web-app> 

我的調度員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" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation=" 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-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/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 

<context:property-placeholder location="classpath:jdbc.properties" /> 

<context:component-scan base-package="net.roseindia" /> 


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

<tx:annotation-driven transaction-manager="hibernateTransactionManager"/> 

<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/view/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

<bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="${database.driver}" /> 
    <property name="url" value="${database.url}" /> 
    <property name="username" value="${database.user}" /> 
    <property name="password" value="${database.password}" /> 
</bean> 

<bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="annotatedClasses"> 
     <list> 
      <value>net.roseindia.model.Article</value> 
     </list> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
      <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
     </props> 
    </property> 

</bean> 

<bean id="hibernatetransactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

我ArticleController.java如下

package net.roseindia.controller; 

import java.util.HashMap; 
import java.util.Map; 

import net.roseindia.model.Article; 
import net.roseindia.service.ArticleService; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.validation.BindingResult; 
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.servlet.ModelAndView; 

@Controller 
@RequestMapping("/articles") 
public class ArticleController { 

    @Autowired 
    private ArticleService articleService; 

    @RequestMapping(value = "/save", method = RequestMethod.POST) 
    public ModelAndView saveArticle(@ModelAttribute(" article") Article article, 
     BindingResult result) { 
    articleService.addArticle(article); 
    return new ModelAndView("redirect:/articles.html"); 
    } 

    @RequestMapping(method = RequestMethod.GET) 
    public ModelAndView listArticles() { 
     Map<String, Object> model = new HashMap<String, Object>(); 
     model.put("articles", articleService.listArticles()); 

    return new ModelAndView("articlesList", model); 
    } 

    @RequestMapping(value = "/add", method = RequestMethod.GET) 
    public ModelAndView addArticle(@ModelAttribute("article") Article article, 
     BindingResult result) { 
    return new ModelAndView("addArticle"); 
    } 

} 

Can有人請幫我解決這個錯誤?

感謝您提前。

感謝, Krunal沙阿

+0

我已經解決了這個問題。 在web.xml我不得不 <歡迎文件>的index.jsp 我改成 <歡迎文件> /WEB-INF/index.jsp 哪個解決了這個請求的資源是不可用的問題。 我認爲這可能會幫助像我這樣的人。當我從網上直接複製這段代碼時,我並不知道我錯過了什麼。 –

回答

0

我建議下面的步驟應解決這一特定問題

右鍵單擊項目 - >屬性 - >部署大會 - >添加

您可以將所有文件夾 (例如Source:/ src/main/java - > Deploy Path:WEB-INF/classes) 添加到特定的Deploy路徑

我認爲這樣會hel一些。

相關問題