Spring MVC实例创建(一)

                                Spring MVC

Spring MVC 为展现底层提供的基于MVC设计理念的优秀的Web框架,是目前最流行的MVC框架之一。
Spring3.0后全面超越Struts2,成为最为优秀的MVC框架。
Spring MVC 通过一套MVC 注解,让POJO成为处理请求的控制器,而无须实现任何接口。
支持REST风格的URL请求。
采用了松散耦合可插拔组件结构,比其他MVC框架更具扩展性和灵活性。

创建步骤:

1、加入jar包:
spring-aop-xxx.jar
spring-beans-xxx.jar
spring-context-xxx.jar
spring-core-xxx.jar
spring-expression.jar
spring-instrument.xxx.jar
spring-web.xxx.jar
spring-webmvc.xxx.jar

commons-logging.xxx.jar  //spring的日志文件

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/jsvaee"
    xsi:schemaLocation="http://java.sun.com.xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <!--配置DispatcherServlet-->
    <servlet>
        <servlet-name>springDispacherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispacherServlet</servlet-class>
        <!--配置DispatcherServlet的一个初始化参数:配置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>
        <!--配置此servlet是在被加载的时候被创而不是在第一次请求的时候被创建-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--配置响应请求的类型-->
    <servlet-mapping>
        <servlet-name>springDispacherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 

3、编写请求处理器:

import com.test.springmvc


@Controller
public class Handler{
        
        /**
        *1、使用@RequestMapping注解来映射请求的URL
        *2、返回值会通过试图解析器解析为实际的物理视图,对于InternalResourceViewResolver视图解析器会做如下的解析:
        *通过prefix+returnVal+后缀 这样的方式得到实际的物理视图,然后会转发操作
        */WEB-INF/view/success.jsp
        *@return
        */
        @RequestMapping("/test")
        public String approve{
            System.out.println("请求处理完成");
            return success;
        }
}

4、配置springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:si="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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd    
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

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

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

5、创建请求页面

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>request</title>
 </head>
 <body>
    <h1>SpringMVC练习</h1>
    <a href="test">请求开始</a>
 </body>
</html>

6、创建响应页面success.jsp

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>request</title>
 </head>
 <body>
    <h1>请求响应成功</h1>
 </body>
</html>

 

posted @ 2017-06-21 14:47  Mr.栋  阅读(216)  评论(0编辑  收藏  举报