码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC入门

时间:2016-02-19 17:11:44      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:

1.1何为SpringMVC?

Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的。

 

另外还有一种基于组件的、事件驱动的Web框架在此就不介绍了,如Tapestry、JSF等。

 

Spring Web MVC也是服务到工作者模式的实现,但进行可优化。前端控制器是DispatcherServlet;应用控制器其实拆为处理器映射器(Handler Mapping)进行处理器管理和视图解析器(View Resolver)进行视图管理;页面控制器/动作/处理器为Controller接口(仅包含ModelAndView handleRequest(request, response) 方法)的实现(也可以是任何的POJO类);支持本地化(Locale)解析、主题(Theme)解析及文件上传等;提供了非常灵活的数据验证、格式化和数据绑定机制;提供了强大的约定大于配置(惯例优先原则)的契约式编程支持。

1.2优点

√让我们能非常简单的设计出干净的Web层和薄薄的Web层;

√进行更简洁的Web层的开发;

√天生与Spring框架集成(如IoC容器、AOP等);

√提供强大的约定大于配置的契约式编程支持;

√能简单的进行Web层的单元测试;

√支持灵活的URL到页面控制器的映射;

√非常容易与其他视图技术集成,如Velocity、FreeMarker等等,因为模型数据不放在特定的API里,而是放在一个Model里(Map数据结构实现,因此很容易被其他框架使用);

√非常灵活的数据验证、格式化和数据绑定机制,能使用任何对象进行数据绑定,不必实现特定框架的API;

√提供一套强大的JSP标签库,简化JSP开发;

√支持灵活的本地化、主题等解析;

√更加简单的异常处理;

√对静态资源的支持;

√支持Restful风格。

 

1.3 请求流程

技术分享

 

图1-1

具体执行步骤如下:

1、  首先用户发送请求————>前端控制器,前端控制器根据请求信息(如URL)来决定选择哪一个页面控制器进行处理并把请求委托给它,即以前的控制器的控制逻辑部分;图1-1中的1、2步骤;

2、  页面控制器接收到请求后,进行功能处理,首先需要收集和绑定请求参数到一个对象,这个对象在Spring Web MVC中叫命令对象,并进行验证,然后将命令对象委托给业务对象进行处理;处理完毕后返回一个ModelAndView(模型数据和逻辑视图名);图1-1中的3、4、5步骤;

3、  前端控制器收回控制权,然后根据返回的逻辑视图名,选择相应的视图进行渲染,并把模型数据传入以便视图渲染;图1-1中的步骤6、7;

4、  前端控制器再次收回控制权,将响应返回给用户,图1-1中的步骤8;至此整个结束。

 

2.1 HelloWorld入门

2.1.1、准备开发环境和运行环境:

☆开发工具:eclipse

☆运行环境:tomcat6.0.20

☆工程:动态web工程(springmvc-chapter2

☆spring框架下载:

spring-framework-3.1.1.RELEASE-with-docs.zip

☆依赖jar包:

1、  Spring框架jar包:

为了简单,将spring-framework-3.1.1.RELEASE-with-docs.zip/dist/下的所有jar包拷贝到项目的WEB-INF/lib目录下;

2、  Spring框架依赖的jar包:

需要添加Apache commons logging日志,此处使用的是commons.logging-1.1.1.jar;

需要添加jstl标签库支持,此处使用的是jstl-1.1.2.jar和standard-1.1.2.jar

 

2.2.1 配置文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 这里的参数如果不配置,则默认查找web-inf下的{servlet-name}-servlet.xml文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>pages/index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

dispatcher-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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd   
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-4.0.xsd 
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-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/task
    http://www.springframework.org/schema/task/spring-task-4.0.xsd">

    <!-- Spring MVC配置 -->
    <context:annotation-config />
    <!--扫描注解 -->
    <context:component-scan base-package="com.lhl.usersystem.action" />
    <!--默认的mvc注解映射的支持 -->
    <mvc:annotation-driven/>
    <!-- 支持异步方法执行 -->
    <task:annotation-driven /> 

    <!-- 视图解析器和json解析器 -->
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html"/>
                <entry key="json" value="application/json"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
                    <property name="prefix" value="/pages/" /> 
                    <property name="suffix" value=".jsp"/>
                </bean>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
            </list>
        </property>
    </bean>
    
    <!-- 文件上传解析器 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="-1"/>
    </bean>

    <!-- 总错误处理 -->
    <bean id="exceptionResolver"
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="defaultErrorView">
            <value>/error</value>
        </property>
        <property name="defaultStatusCode">
            <value>500</value>
        </property>
        <property name="warnLogCategory">
            <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
            </value>
        </property>
    </bean>

    <!-- 对静态资源文件的访问 -->
    <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926" />
    <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926" />
    <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926" />
</beans>

 

applicationContext.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd   
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-4.0.xsd 
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-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/task
    http://www.springframework.org/schema/task/spring-task-4.0.xsd">

    <!-- Spring MVC配置 -->
    <context:annotation-config />
    <!--扫描注解 -->
    <context:component-scan base-package="com.lhl.usersystem.service" />
    <!--默认的mvc注解映射的支持 -->
    <mvc:annotation-driven/>
    <!-- 支持异步方法执行 -->
    <task:annotation-driven /> 

    <!-- 数据库和事务配置 -->

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!-- 定义数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass">
            <value>${jdbc.driverClassName}</value>
        </property>
        <property name="jdbcUrl">
            <value>${jdbc.url}</value>
        </property>
        <property name="user">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
        <!--连接池中保留的最小连接数。 -->
        <property name="minPoolSize">
            <value>${jdbc.cpool.minPoolSize}</value>
        </property>
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize">
            <value>${jdbc.cpool.maxPoolSize}</value>
        </property>
        <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize">
            <value>${jdbc.cpool.initialPoolSize}</value>
        </property>
    </bean>

    <!-- 定义jdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <constructor-arg><ref bean="dataSource"/></constructor-arg> 
    </bean>

    <!-- 定义事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>

    <!-- 配置事务特性 ,配置add、delete和update开始的方法,事务传播特性为required -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="login*" propagation="REQUIRED" />
            <tx:method name="regist*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 配置那些类的方法进行事务管理 -->
    <aop:config>
        <aop:pointcut id="allManagerMethod" expression="execution (* com.lhl.usersystem.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
    </aop:config>
</beans>

 

2.2.2 jsp

index.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<%@ include file="/commons/taglibs.jsp" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>首页</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
var login = "<%=request.getParameter("login")%>";
var register = "<%=request.getParameter("register")%>";
function loadHandler() {
    if (login == "failed") {
        document.getElementById("loginTip").style.display = "";
        document.getElementById("loginMsg").innerHTML = "用户名或密码错误";
    } else if (login == "mustlogin") {
        document.getElementById("loginTip").style.display = "";
        document.getElementById("loginMsg").innerHTML = "请先登录再访问其他页面";
    }
    if (register == "success") {
        document.getElementById("registerTip").style.display = "";
        document.getElementById("tipMsg").innerHTML = "注册成功,可以去登录了";
    } else if (register == "exists") {
        document.getElementById("registerTip").style.display = "";
        document.getElementById("tipMsg").innerHTML = "用户名已经存在";
    }
}
</script>
</head>

<body onload="loadHandler()">
    <table style="margin: 50px;" align="center">
        <tr>
            <td>
            <form action="${ctx}/user/login.do" method="post">
                <table>
                    <tr>
                        <td colspan="2">已经有帐号,登录</td>
                    </tr>
                    <tr>
                        <td>用户名</td>
                        <td><input type="text" name="username" /></td>
                    </tr>
                    <tr>
                        <td>密码</td>
                        <td><input type="password" name="password" /></td>
                    </tr>
                    <tr id="loginTip" style="color: red;display: none;">
                        <td colspan="2" id="loginMsg">用户名或密码错误</td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" value="登录" /></td>
                    </tr>
                </table>
                </form>
            </td>
            <td style="padding: 100px;">
            <form action="${ctx }/user/register.do" method="post">
                <table>
                    <tr>
                        <td colspan="2">还没有帐号,注册</td>
                    </tr>
                    <tr>
                        <td>用户名</td>
                        <td><input type="text" name="username" /></td>
                    </tr>
                    <tr>
                        <td>密码</td>
                        <td><input type="password" name="password" /></td>
                    </tr>
                    <tr id="registerTip" style="color: red;display: none;">
                        <td id="tipMsg" colspan="2"></td>
                    </tr>
                    <tr>
                        <td colspan="2"><input type="submit" value="注册" /></td>
                    </tr>
                </table>
                </form>
            </td>
        </tr>
    </table>
</body>
</html>

 

2.2.3 Action

UserAction.java

package com.lhl.usersystem.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lhl.usersystem.service.UserService;

@Controller
@RequestMapping("/user")
public class UserAction {
    
    @Resource
    private UserService userService;

    @RequestMapping("/login")
    public ModelAndView login(@RequestParam("username") String username, @RequestParam("password") String password, HttpServletRequest req) {
        Map result = userService.checkLogin(username, password);
        if (result != null) {
            String token = DigestUtils.md5DigestAsHex(req.getSession().getId().getBytes());
            req.getSession().setAttribute("OnlineUser", result);
            req.getSession().setAttribute("token", token);
            return new ModelAndView("redirect:/pages/welcome.jsp");
        }
        return new ModelAndView("redirect:/pages/index.jsp?login=failed");
    }

    @RequestMapping("/save")
    public String save(@RequestParam("id") String id,
            @RequestParam("token") String token,
            @RequestParam("name") String name,
            @RequestParam("sex") String sex,
            @RequestParam("birthday") String birthday,
            HttpServletRequest req) {
        String sesstoken = DigestUtils.md5DigestAsHex(req.getSession().getId().getBytes());
        if (!StringUtils.equals(sesstoken, token)) {
            return "redirect:/pages/welcome.jsp?save=invalidtoken";
        }
        userService.save(id, name, sex, birthday);
        Map result = userService.get(id);
        req.getSession().setAttribute("OnlineUser", result);
        
        return "redirect:/pages/welcome.jsp?save=success";
    }
    
    @RequestMapping("/logout")
    public String logout(HttpServletRequest req) {
        req.getSession().removeAttribute("OnlineUser");
        req.getSession().removeAttribute("token");
        return "redirect:/pages/index.jsp";
    }
    @RequestMapping("/register")
    public String register(@RequestParam("username") String username,
            @RequestParam("password") String password) {
        Map result = userService.getByUsername(username);
        if (result != null) {
            return "redirect:/pages/index.jsp?register=exists";
        }
        userService.register(username, password);
        return "redirect:/pages/index.jsp?register=success";
    }
}

 

2.2.4 jsp

welcome.jsp

<%@ page language="java" pageEncoding="utf-8"%>
<%@ include file="/commons/taglibs.jsp" %>
<%
Object user = request.getSession().getAttribute("OnlineUser");
if (user == null) {
    response.sendRedirect(request.getContextPath() + "/pages/index.jsp?login=mustlogin");
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Welcome</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="${ctx}/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
var save = "<%=request.getParameter("save")%>";
function loadHandler() {
    if (save == "success") {
        document.getElementById("saveTip").style.display = "";
        document.getElementById("tipMsg").innerHTML = "保存成功";
    } else if (save == "invalidtoken") {
        document.getElementById("saveTip").style.display = "";
        document.getElementById("tipMsg").innerHTML = "Token无效";
    }
}
</script>
</head>

<body onload="loadHandler()">
    <div align="center" style="font-size: 30px">Welcome</div>
    <div align="center" style="padding-top:100px;">
        修改个人信息 <a href="${ctx }/user/logout.do">退出</a>
        <form action="${ctx}/user/save.do" method="post">
        <table>
            <tr>
                <td>Token</td>
                <td><input type="text" name="token" value="${token}" readonly="readonly" style="color: gray;border: 0px" /></td>
            </tr>
            <tr>
                <td>用户ID</td>
                <td><input type="text" name="id" value="${OnlineUser.id }" readonly="readonly" style="color: gray;border: 0px" /></td>
            </tr>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="username" value="${OnlineUser.username }" readonly="readonly" style="color: gray;border: 0px" /></td>
            </tr>
            <tr>
                <td>姓名</td>
                <td><input type="text" name="name" value="${OnlineUser.name }" /></td>
            </tr>
            <tr>
                <td>性别</td>
                <td>
                <label><input type="radio" name="sex" value="man"  <c:if test="${OnlineUser.sex == ‘man‘}">checked="checked"</c:if> /></label>
                <label><input type="radio" name="sex" value="woman" <c:if test="${OnlineUser.sex == ‘woman‘}">checked="checked"</c:if> /></label>
                </td>
            </tr>
            <tr>
                <td>生日</td>
                <td><input type="text" readonly="readonly" name="birthday" value="${OnlineUser.birthday }"  onFocus="WdatePicker()" /></td>
            </tr>
            <tr id="saveTip" style="color: red;display: none;">
                <td colspan="2" id="tipMsg">保存成功</td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="保存" /></td>
            </tr>
        </table>
        </form>
    </div>
</body>
</html>

 

SpringMVC入门

标签:

原文地址:http://www.cnblogs.com/zyxiaohuihui/p/5201605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!