春天还没有解决404(如何解决)

时间:2017-04-25 19:58:00

标签: spring

这是我的代码(Web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/WEB-INF/pages/*</url-pattern>
    </servlet-mapping>
</web-app>

这是我的配置文件

<?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-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:component-scan base-package="com.srccodes.spring.controller" />
    <mvc:annotation-driven />


  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean> 

</beans>

的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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>HelloWorld</groupId>
  <artifactId>HelloWorld</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>HelloWorld Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
        <org.springframework.version>3.1.2.RELEASE</org.springframework.version>
    </properties>

    <dependencies>
        <!-- Spring MVC depends on spring-core, spring-beans, spring-context, spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>SpringMVCHelloWorld</finalName>
        <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        </plugins>
        </build>

</project>

我的控制器类

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; 

    @Controller
       public class SpringMVCHelloController {

           @RequestMapping("/")
           public String printHelloWorld(Model model) {
               model.addAttribute("message", "Hello World!");

               return "helloWorld";
           }
       }

1 个答案:

答案 0 :(得分:0)

您的应用有三件事需要更改才能开始工作。

添加包声明tp SpringMVCHelloController:

email.Send()

如果没有包声明,控制器将使用默认包,并且在组件扫描时不会被发现。

添加视图文件/WEB-INF/pages/helloWorld.jsp,例如:

package com.srccodes.spring.controller;

没有视图,将无法显示任何内容。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored ="false"%> <!DOCTYPE html> <html> <head> <title>My page</title> </head> <body> Message: ${message} </body></html> servlet的servlet-mapping url-pattern更改为:

dispatcher

因此请求将正确分派给您的SpringMVCHelloController,并且返回的视图将被正确解析和处理。