0

请帮我解决不难的问题。我想在我的项目中尝试使用 @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 (其他控制器和网络应用程序工作正常)

<?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 错误资源不可用。请帮助我,谢谢!

4

2 回答 2

0

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

<mvc:annotation-driven />

<mvc:annotation-driven /> 有什么作用?

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

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

于 2013-06-19T11:12:56.163 回答
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

于 2013-06-19T13:18:19.770 回答