天天看点

一、SpringMVC入门,实现HelloWorld

步骤:

- 加入 jar 包

- 在 web.xml 中配置 DispatcherServlet

- 加入 Spring MVC 的配置文件

- 编写处理请求的处理器,并标识为处理器– 编写视图

1.1添加SpringMVC开发的相关jar包

commons-logging-1.1.3.jar

spring-aop-4.0.0.RELEASE.jar

spring-beans-4.0.0.RELEASE.jar

spring-context-4.0.0.RELEASE.jar

spring-core-4.0.0.RELEASE.jar

spring-expression-4.0.0.RELEASE.jar

spring-web-4.0.0.RELEASE.jar

spring-webmvc-4.0.0.RELEASE.jar

1.2配置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" id="WebApp_ID" version="3.0">
  <display-name>springmvc-1</display-name>

  <!-- 配置DispatcherServlet -->
  <servlet>
     <servlet-name>DispatcherServlet</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!-- 配置DispacherServlet的一个初始化参数:配置SpringMVC配置文件的位置和名称 -->
     <!-- 实际上不用通过contextConfigLocation来配置springMVC的配置文件,而是用默认的 -->
     <!-- 
         默认的配置文件为:WEB-INF/<servlet-name>-servlet.xml

     <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:springmvc.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>
</web-app>
           

1.3创建SpringMVC配置文件DispatcherServlet-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: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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>

    <!-- 配置视图解析器 :如何把handler 方法返回值解析为实际的物理视图-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/views/"></property>
       <property name="suffix" value=".jsp"></property>
    </bean>

</beans>
           

1.4创建请求处理器类

package com.atguigu.springmvc.handlers;

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

@Controller
public class HelloWorld {


    /*1.使用 @RequestMapping注解来映射请求的URL
     *2.返回值会通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver视图解析器,会做如下的解析
     *通过(prefix+returnValue+后缀)得到实际的物理视图,然后做转发操作
     * /WEB-INF/views/success.jsp
     */
    @RequestMapping("/helloworld")
    public String hello()
    {
        System.out.println("hello world");
        return "success";
    }

}
           

1.5创建相应的视图

index.jsp

<body>
    <a href="helloworld">Hello World</a>
  </body>
           

success.jsp

继续阅读