设置Spring mvc

时间:2015-06-09 00:54:57

标签: java spring maven spring-mvc

我从Spring MVC开始,我正在学习一些教程,我遗漏了一些内容,但我无法看到它是什么。 这是我得到的: 文件:pom.xml中

<project xmlns="http://maven.apache.org/POM/4.0.0"                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0         http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wearedevelopers</groupId>
  <artifactId>contabills</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.3</version>
    </dependency>
</dependencies>
<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

文件:contabills-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:p="http://www.springframework.org/schema/p"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.wearedevelopers.controllers" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    <property name="prefix" value="/WEB-INF/templates/" />  
    <property name="suffix" value=".html" />  
</bean> 

文件: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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>ContaBills</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

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

</web-app>

文件:HomeController

@Controller
@RequestMapping("/home")
public class HomeController {
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String helloWorld(Model model)
    {
        model.addAttribute("mensagem", "mensagem");
        return "home";
    }
}

更新: 在控制台中,其中一条消息是: [SetPropertiesRule] {服务器/服务/引擎/主机/上下文}设置属性&#39; source&#39; to&#39; org.eclipse.jst.jee.server:contabills&#39;找不到匹配的财产。

当我请求URL localhost:8080 // home / index时,我希望文件home.html,我已定义,但我得到了404。 我相信我错过了一些东西,我无法看到它是什么。

编辑: 此外,在控制台中有这个:

信息:没有在类路径上检测到Spring WebApplicationInitializer类型

3 个答案:

答案 0 :(得分:0)

根据web.xml中的servlet映射,您的requestmapping应该具有.html扩展名。

因此在应用程序中的请求映射中使用.html扩展名。这应该有用。

label

答案 1 :(得分:0)

你应该修两行。 第一个是在web.xml&#34; spring-mvc&#34;而不是spring-mvc使用&#34; contabills&#34; 。 第二个是你应该总是使用.html文件来处理RequestMappings,因为在web.xml中你将所有请求html请求过滤到spring dispatcher servlet。 我强烈建议在类路径中添加一个log4j配置文件,该文件配置为级别调试。您可以轻松跟踪请求的所有步骤。

答案 2 :(得分:-1)

您的网址格式错误,并且您错过了注释驱动语句。顺便说一句,在*之后加classpath。您的文件应该是:

<强>的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<servlet>
   <servlet-name>DispatcherServlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:contabills-servlet.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>DispatcherServlet</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

<强> contabills-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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.wearedevelopers.controllers" />
<mvc:annotation-driven />

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    <property name="prefix" value="/WEB-INF/templates/" />  
    <property name="suffix" value=".html" />  
 </bean>
</beans>

这应该有用。