RequestMapping不起作用

时间:2013-06-19 11:00:54

标签: spring jsp

请帮助我解决不难的问题。我想在我的项目中尝试使用@RequestMapping注释。我创建了WebController.java,它有

  package com.mycompany.controller.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;

    @Controller
    @RequestMapping("/welcome")
    public class WebController{

        @RequestMapping(method = RequestMethod.GET)
        public ModelAndView helloWorld(){

            ModelAndView model = new ModelAndView("HelloWorldPage");
            model.addObject("msg", "hello world");

            return model;
        }
    }

和HelloWorldPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${msg }
</body>
</html>

springmvc-servlet.xml(其他控制器和Web应用程序正常工作)

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

     <context:annotation-config />

    <context:component-scan base-package="com.mycompany.controller" />

    <bean name="/countrylist.html" class="com.mycompany.controller.controller.CountryController"/>
    <bean name="/citylist.html" class="com.mycompany.controller.controller.CityController"/>
    <bean name="/successcitysearch.html" class="com.mycompany.controller.controller.CitiesOfCountryController"/>

    <bean name="/addcountry.html" class="com.mycompany.controller.controller.AddCountryController">
    <property name="commandClass" value="com.mycompany.model.domain.Country"/>
    <property name="formView" value="addcountry"/>
    <property name="successView" value="countrylist.html"/>
    </bean>

    <bean name="/addcity.html" class="com.mycompany.controller.controller.AddCityController">
    <property name="commandClass" value="com.mycompany.model.domain.City"/>
    <property name="formView" value="addcity"/>
    <property name="successView" value="citylist.html"/>
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>



    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
        p:password="${jdbc.password}" />


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>/WEB-INF/hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app 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" >

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
    </context-param>
     <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>


  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>

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

</web-app>

所以当我尝试编写链接localhost / MyCountryProject / welcome或localhost / MyCountryProject / welcome.html时,它给了我404错误资源不可用。请帮助我,谢谢!

2 个答案:

答案 0 :(得分:0)

要使用带注释的spring-mvc,您需要在配置中告诉spring。

<mvc:annotation-driven />

What does <mvc:annotation-driven /> do?

还要确保您的文件路径正确。

使搜索错误更容易的一件事是记录调试信息。日志将告诉您到底发生了什么以及如何发生。

答案 1 :(得分:0)

我认为这个问题与servlet映射配置有关。 改变@RequestMapping:

@RequestMapping("/welcome.html")

和servlet映射应该是这样的:

 <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/welcome.html</url-pattern>
    </servlet-mapping>

链接将是localhost / MyCountryProject / welcome.html

相关问题