【问题标题】:Setting up Spring mvc设置 Spring mvc
【发布时间】:2015-06-09 00:54:56
【问题描述】:

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

文件:家庭控制器

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

更新: 在控制台中,其中一条消息是: [SetPropertiesRule]{Server/Service/Engine/Host/Context} 将属性 'source' 设置为 'org.eclipse.jst.jee.server:contabills' 没有找到匹配的属性。

当我请求 URL localhost:8080//home/index 时,我期望文件 home.html,这是我定义的,但我得到了 404。 我相信我遗漏了一些东西,我看不到它是什么。

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

信息:在类路径中未检测到 Spring WebApplicationInitializer 类型

【问题讨论】:

  • 问题是我得到 404。应用程序没有映射我的页面。
  • 有人能解释一下为什么对更多问题投反对票吗?
  • 因为您没有(或没有)解释您期望的行为、您期望该行为的原因以及您得到的实际行为。你发了什么请求?对于什么网址? Spring MVC 应用程序的哪个部分应该处理该请求?等等。
  • 谢谢@SotiriosDelimanolis。我会编辑我的帖子。
  • 因为这些问题的答案越来越无聊,所以这里是简短版本:您缺少 MVC 配置,通常使用 &lt;mvc:annotation-driven /&gt;。您的 servlet 配置搞砸了。您声明了一个名为spring-mvc 的Servlet,但随后为名为@9​​87654327@ 的Servlet 声明了一个servlet 映射。最后,您的 servlet 映射到 *.html,但您将请求发送到 .../index

标签: java spring maven spring-mvc


【解决方案1】:

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

所以在应用程序的请求映射中使用 .html 扩展名。这应该可以。

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

【讨论】:

  • 您好,没有用,此外,还有一个用于扩展 HTML 的模板解析器。
  • 请在帖子中查看我的编辑。我相信我的文件 web.xml 无法识别。
【解决方案2】:

您应该修复两行。 第一个是在 web.xml "spring-mvc" 里面,而不是 spring-mvc use "contabills" 。 第二个是您应该始终使用 .html 文件作为 RequestMappings,因为在 web.xml 中,您将所有请求 html 请求过滤到 spring 调度程序 servlet。 我强烈建议在您的类路径中添加一个 log4j 配置文件,该文件配置为级别调试。您可以轻松跟踪请求的所有步骤。

【讨论】:

  • 完成。但我什至没有机会记录一些东西。它没有进入控制器。
【解决方案3】:

您的 url 模式错误,您缺少注释驱动语句。顺便说一句,在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>

这应该可行。

【讨论】:

  • 是的。请参阅我有问题的编辑。我相信我的文件 web.xml 无法识别。
猜你喜欢
  • 2014-08-09
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 2015-12-24
  • 1970-01-01
相关资源
最近更新 更多