简体   繁体   English

设置 Spring mvc

[英]Setting up Spring mvc

I am starting with Spring MVC and I am following some tutorials, I am missing something but I can't see what it is.我从 Spring MVC 开始,我正在学习一些教程,我遗漏了一些东西,但我看不到它是什么。 Here is what I got: file:pom.xml这是我得到的:file: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>

File:contabills-servlet.xml文件: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> 

File: web.xml文件: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>

File: HomeController文件:家庭控制器

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

UPDATE: In console, one of the message is: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:contabills' did not find a matching property.更新:在控制台中,消息之一是:[SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:contabills' 没有找到匹配的属性.

When I request the URL localhost:8080//home/index, I expect the file home.html, wich I have defined, but I got 404. I believe I am missing something and I can't see what it is.当我请求 URL localhost:8080//home/index 时,我期望文件 home.html,我已经定义了,但我得到了 404。我相信我遗漏了一些东西,我看不到它是什么。

EDIT: Also, there is this in console:编辑:另外,控制台中有这个:

INFO: No Spring WebApplicationInitializer types detected on classpath信息:在类路径上未检测到 Spring WebApplicationInitializer 类型

Your requestmapping should have .html extension according to your servlet mapping in web.xml.根据您在 web.xml 中的 servlet 映射,您的 requestmapping 应该具有 .html 扩展名。

So use .html extension in requestmapping in application.所以在应用程序中的请求映射中使用 .html 扩展名。 This should work.这应该有效。

@RequestMapping(value = "/index.html", method = RequestMethod.GET)
    public String helloWorld(Model model)
    {

You should fix two lines.你应该修复两行。 1st is inside web.xml "spring-mvc" instead of spring-mvc use "contabills" .第一个是在 web.xml "spring-mvc" 而不是 spring-mvc 使用 "contabills" 。 2nd is you should always use .html files for RequestMappings because in web.xml you filter all requests html requests to spring dispatcher servlet.第二是您应该始终为 RequestMappings 使用 .html 文件,因为在 web.xml 中,您将所有请求 html 请求过滤到 spring 调度程序 servlet。 And i strongly suggest to add a log4j configuration file inside your classpath which is configured to level debug.我强烈建议在您的类路径中添加一个 log4j 配置文件,该文件配置为级别调试。 You can easily trace all steps of your request.您可以轻松跟踪请求的所有步骤。

Your url-pattern is wrong and you're missing the annotation driven statement.您的 url-pattern 是错误的,并且您缺少注释驱动的语句。 By the way, put a * after classpath .顺便说一下,在classpath后面放一个* Your files should be like:你的文件应该是这样的:

web.xml网页.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 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>

This should work.这应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM