当我尝试使用STS运行Spring MVC时,我得到请求的资源()不可用

时间:2011-10-11 23:24:09

标签: hibernate spring-mvc

我是Spring MVC和Spring / Hibernate的新手。

我正在尝试运行我在网上找到的示例尝试执行此操作并且无法成功运行它。

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

我的dispatcher-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");
  }

}

有人可以帮我解决这个错误吗?

提前感谢你。

谢谢, Krunal Shah

1 个答案:

答案 0 :(得分:0)

我建议以下步骤应该解决这个具体问题

右键点击该项目 - &gt;属性 - &gt;部署组件 - &gt;添加

您可以添加所有文件夹 (例如Source:/ src / main / java - &gt; Deploy Path:WEB-INF / classes) 到特定的部署路径

我认为这会有所帮助。