天天看点

[读后感]spring Mvc 教程框架实例以及系统演示下载

[读后感]spring Mvc 教程框架实例以及系统演示下载

<a target="_blank" href="http://blog.csdn.net/opengl_es">转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。</a>

不要好意思,昨晚写的,睡着忘发了,后附是篇好文,赶紧w分享一下。

感脚着,俺好像做了件聪明事儿,却不知会不会遭到不理解。

转载的好文,是不会推荐到首页的,所以,用这种读后感的方式,引用好文,发表读后之感。

当然,不是随意编造,而是确有实感,而且好文,确实也不是能出来的感受,请向后看吧。

---------------

这篇没什么好说的,直接照做,无论是本地 Tomcat 6 ,还是 SAE 都能正常运行。

之前下了个处理微信的源码,想搞个个人微信发布,发些微信上转载的精品文章,谁想确没那么容易。

看来就得一步一步地试试了。

首先、使用 Eclipse 建个动态 Web 工程,加入个 index.jsp,发布 War 包到 Tomcat 6,测试成功,再上传到 SAE,也成功了;

然后、找来下文,逐步照做,并下载附件中 JAR 包加入,继续发布,也成功了;

接着、还不知道要做什么,是研究一下 MyBatis ,还是研究一下 Spring MVC 的深入配置与注解处理规则?

弄着玩的东西,也无所谓了,走哪算哪儿,有闲工夫,就花上半小时,玩一玩,比玩游戏要强一些吧,玩游戏锻炼身体的耐力,

玩这东西,拿着 11寸 Mac Book Air ,你是想躺着,还是卧着,又或趴着,反正一指禅足够用了,对身体基本没有要求。

话说,研究这东西,虽然工作中用不到,但至少看别人写的东西,疏理个大体逻辑,还是没问题的,有助于工作上沟通和问题解决,何乐不为呢。

希望此文能给爱玩的兄弟一些参照,借超有爱的原作者的花,献给大家吧!

[读后感]spring Mvc 教程框架实例以及系统演示下载

发表时间:2014-09-04 11:31 | coding云 | 3320次阅读 | 21条评论

      今天coding云为大家带来spring mvc框架的实例教程,该教程没有复杂的内容,不需要了解spring mvc的运行机制,就可以搭建好框架,继而进行快速开发,以此来体会spring mvc框架的简单、易用、方便以及安全的各种特性。事先说明,本文没有过多的理论内容,但是当您搭建好这套框架,慢慢的就能体会到其中包含的理论知识了哦。

      配置文件我都为大家准备好了,而且有源码包可以下载。这样就能方便您的快速学习,等您在自己的机器搭建好该项目,跑起来本文提供的简单例子后,您可以看看我们的源码,注释都很详细,通过例子去理解这些配置,这样就能帮助您快速理解spring mvc的一些运行机制,从而掌握这套优秀的框架。

      顺便告诉大家一声,coding云就是建立在该例子之上的哦,期待您也能成长为拥有类似coding云这样站点的大牛哦。ok,下面开始着手搭建框架。

一、准备eclipse开发环境

      eclipse是java程序员必备的开发环境,如果您还在用MyEclipse的话,我只能强烈建议您开始使用eclipse,作为一个java程序员,如果没有用过eclipse的话,那就不是一个合格的java程序员,具体原因请百度。

      我为大家准备的是eclipse4.3版本,只安装了常用的插件,比如svn版本控制以及maven等,完全从官方的最初版本安装插件后的版本。大家可以到我的网盘下载,下面是免费共享链接:

http://yunpan.cn/Q7ws623WhxEZg  提取码 ca2c

至于jdk的环境变量等(java程序员必须懂),请自行google、百度。

二、eclipse下新建项目

1.  打开eclipse后,点击【File】--》【new】,弹出窗口,选择【web】--》【Dynamic Web project】

2. 一路next,在新打开的窗口,输入项目名称,本例中输入的是codingyun-demo,继续点击next,最后点击finish,项目列表中就能看到我们的新项目codingyun-demo了.

3. 导入spring mvc相关的jar包到lib下

首先下载jar包,我已经把相关jar包都准备好了,大家可以到网盘直接下载:

http://yunpan.cn/Q7wyWjWYfW5Wq  提取码 d2c4

解压缩后,将所有jar包放到项目的【WebContent】目录下的【WEB-INF】目录下的lib目录下,直接复制粘贴进去就ok

4.配置项目引用刚才导入到lib下的jar包

右键点击项目名,在菜单中选择【Build Path】--》【Configuration Build Path...】

三、新增spring mvc的配置文件

找到项目目录下的【Java Resources】,右键点击【Java Resources】,在弹出窗口选择【new】--》【Source folder】,

source folder建好之后,需要把配置文件复制粘贴进去,如下图:

这四个文件分别是:

database.properties:配置数据库连接的地址,用户名以及密码

jdbc-context.xml:spring mvc的jdbc连接数据库的bean的配置

springMvc3-servlet.xml:spring mvc的其他配置(非常重要)

log4j.properties:控制台打印日志的配置、输出日志文件的配置

1. database.properties的配置:注意替换shujuku_name为你的数据库名称,还有用户名、密码

mysql.driverclass=com.mysql.jdbc.Driver

mysql.jdbcurl=jdbc:mysql://127.0.0.1:3306/shujuku_name?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf8

mysql.user=root

mysql.password=abc123

2. jdbc-context.xml的配置:暂时未加事务控制等配置,本次实例为了简单一些。如有需要,请百度或者给我留言,我会补充上来

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;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:aop="http://www.springframework.org/schema/aop"  

    xmlns:tx="http://www.springframework.org/schema/tx"  

    xmlns:context="http://www.springframework.org/schema/context"  

    xmlns:cache="http://www.springframework.org/schema/cache"

    xsi:schemaLocation="   

          http://www.springframework.org/schema/beans   

          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd   

          http://www.springframework.org/schema/tx   

          http://www.springframework.org/schema/tx/spring-tx-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/aop   

          http://www.springframework.org/schema/aop/spring-aop-3.1.xsd" default-autowire="byName"&gt;

     &lt;!-- 创建数据源   commons-dbcp.jar;     commons-pool.jar包--&gt;

     &lt;context:property-placeholder location="classpath:database.properties"/&gt;

&lt;bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"&gt;

&lt;property name="driverClassName" value="${mysql.driverclass}" /&gt;

&lt;property name="url" value="${mysql.jdbcurl}" /&gt;

&lt;property name="username" value="${mysql.user}" /&gt;

&lt;property name="password" value="${mysql.password}" /&gt;

&lt;/bean&gt;

    &lt;bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"&gt;

&lt;constructor-arg&gt;&lt;ref bean="dataSource"/&gt;&lt;/constructor-arg&gt;

&lt;/bean&gt; 

    &lt;!-- 看了spring源码,该 org.springframework.jdbc.core.JdbcTemplate类的继承类有dataSource属性,所以可以属性注入--&gt;

     &lt;bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"&gt;

    &lt;property name="dataSource"&gt;

        &lt;ref bean="dataSource"/&gt;

    &lt;/property&gt;

     &lt;/bean&gt;

&lt;/beans&gt;

3. springMvc3-servlet.xml的配置:

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  

    xmlns:mvc="http://www.springframework.org/schema/mvc"  

           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"&gt;

    &lt;!-- 启动扫描所有的controller --&gt;

    &lt;context:component-scan base-package="com.codingyun.core.action"/&gt;

    &lt;!--  主要作用于@Controller,激活该模式

    下面是一种简写形式,完全可以手动配置替代这种简写形式;

     它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,

       是spring MVC为@Controllers分发请求所必须的

     --&gt;

&lt;!--     &lt;mvc:annotation-driven/&gt; --&gt;

    &lt;mvc:annotation-driven&gt;

        &lt;mvc:message-converters register-defaults="true"&gt;

            &lt;bean class="org.springframework.http.converter.StringHttpMessageConverter"&gt;

                &lt;constructor-arg value="UTF-8" /&gt;

            &lt;/bean&gt;

        &lt;/mvc:message-converters&gt;

    &lt;/mvc:annotation-driven&gt;

    &lt;!-- 配置js,css等静态文件直接映射到对应的文件夹,不被DispatcherServlet处理 --&gt;

    &lt;mvc:resources location="/WEB-INF/resources/**" mapping="/resources"/&gt;

    &lt;mvc:resources mapping="/javascript/**" location="/static_resources/javascript/"/&gt;  

    &lt;mvc:resources mapping="/styles/**" location="/static/css/"/&gt;  

    &lt;mvc:resources mapping="/images/**" location="/static/images/"/&gt; 

    &lt;!-- 配置页面访问地址www.xxx.com/about返回的静态html文件 --&gt;

    &lt;mvc:resources mapping="/about/**" location="/WEB-INF/html/"/&gt;

    &lt;!-- 走servlet的默认配置,先走默认的web.xml配置的servlet,没有的话才找对应controller --&gt;

    &lt;mvc:default-servlet-handler /&gt;

    &lt;!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在jsp/目录下,查找XXX.jsp文件--&gt;

    &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;

    &lt;property name="prefix" value="/WEB-INF/jsp/"&gt;&lt;/property&gt;

    &lt;property name="suffix" value=".jsp"&gt;&lt;/property&gt;

    &lt;/bean&gt;

    &lt;bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;

&lt;property name="defaultEncoding"&gt;

&lt;value&gt;UTF-8&lt;/value&gt;

&lt;/property&gt;

&lt;property name="maxUploadSize"&gt;

&lt;value&gt;32505856&lt;/value&gt;&lt;!-- 上传文件大小限制为31M,31*1024*1024 --&gt;

&lt;property name="maxInMemorySize"&gt;

&lt;value&gt;4096&lt;/value&gt;

4. log4j.properties的配置:

log4j.rootLogger=INFO,console,R

log4j.appender.console=org.apache.log4j.ConsoleAppender

log4j.appender.console.Target=System.out

log4j.appender.console.layout=org.apache.log4j.PatternLayout

log4j.appender.console.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n

log4j.appender.console.threshold=INFO

log4j.appender.R=org.apache.log4j.RollingFileAppender

log4j.appender.R.layout=org.apache.log4j.PatternLayout

log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n

log4j.appender.R.File=/alidata/zkyj/web/zkyj-web.log

log4j.appender.R.Append=true

log4j.appender.R.Threshold=INFO

log4j.appender.R.MaxFileSize=20MB

log4j.appender.LOGFILE.MaxBackupIndex=100

log4j.org.springframework.web=INFO

四、配置web.xml,以及新建jsp、css、js等目录

依照上图,新建目录,以及web.xml文件

web.xml的配置

&lt;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"&gt;

&lt;display-name&gt;zkyj-web&lt;/display-name&gt;

&lt;!-- web.xml 的加载顺序是:context-param -&gt; listener -&gt; filter -&gt; servlet ,

而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的 --&gt;

&lt;!-- spring上下文载入监听器,确保web服务启动时,spring已经完成初始化 --&gt;

&lt;listener&gt;

  &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;

&lt;/listener&gt;

&lt;context-param&gt;

&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;

&lt;param-value&gt;classpath:jdbc-context.xml&lt;/param-value&gt;

&lt;/context-param&gt;  

&lt;filter&gt;

&lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;

&lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;

&lt;init-param&gt;

&lt;param-name&gt;encoding&lt;/param-name&gt;

&lt;param-value&gt;UTF-8&lt;/param-value&gt;

&lt;/init-param&gt;

&lt;param-name&gt;ForceEncoding&lt;/param-name&gt;

&lt;param-value&gt;true&lt;/param-value&gt;

&lt;/filter&gt;

&lt;filter-mapping&gt;

&lt;url-pattern&gt;/*&lt;/url-pattern&gt;

&lt;/filter-mapping&gt;

&lt;servlet&gt;

&lt;servlet-name&gt;springMvc3&lt;/servlet-name&gt;

&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;

&lt;param-value&gt;classpath:springMvc3-servlet.xml&lt;/param-value&gt;

&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;

&lt;/servlet&gt;

    &lt;!-- 会话超时配置(单位为分钟) --&gt;

&lt;session-config&gt; 

    &lt;session-timeout&gt;120&lt;/session-timeout&gt; 

&lt;/session-config&gt;

&lt;servlet-mapping&gt;     

&lt;servlet-name&gt;default&lt;/servlet-name&gt;    

&lt;url-pattern&gt;*.jpg&lt;/url-pattern&gt;       

&lt;/servlet-mapping&gt;      

&lt;servlet-mapping&gt;         

&lt;servlet-name&gt;default&lt;/servlet-name&gt;      

&lt;url-pattern&gt;*.js&lt;/url-pattern&gt;      

&lt;servlet-mapping&gt;          

&lt;servlet-name&gt;default&lt;/servlet-name&gt;         

&lt;url-pattern&gt;*.css&lt;/url-pattern&gt;        

&lt;/servlet-mapping&gt;   

&lt;servlet-mapping&gt;

&lt;url-pattern&gt;/&lt;/url-pattern&gt;

&lt;/servlet-mapping&gt;    

&lt;welcome-file-list&gt;

&lt;welcome-file&gt;index.html&lt;/welcome-file&gt;

&lt;/welcome-file-list&gt;

&lt;!-- 通过错误码来配置error-page ,配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp。 --&gt;

&lt;error-page&gt; 

    &lt;error-code&gt;404&lt;/error-code&gt; 

    &lt;location&gt;/page404.html&lt;/location&gt; 

&lt;/error-page&gt; 

&lt;!--  通过异常的类型配置error-page ,配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp  --&gt;

    &lt;exception-type&gt;java.lang.NullPointerException&lt;/exception-type&gt; 

    &lt;location&gt;/WEB-INF/jsp/error.jsp&lt;/location&gt; 

&lt;/error-page&gt;

&lt;/web-app&gt;

五、新建controller,接收页面请求,返回相应的页面

在项目的src目录下新建包com.codingyun.core.action

在该包下新建indexController.java

indexController.java内容:

package com.codingyun.core.action;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

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

import org.springframework.web.bind.annotation.RequestMethod;

@Controller

public class IndexController{

/**

* 进入demo首页

* @user jack

* 2014年9月4日

*/

@RequestMapping(value = "", method = RequestMethod.GET)

public String index(HttpServletRequest request){

//对应的是WebContent目录下的WEB-INF目录下的jsp目录下的demo下的index.jsp

//请查看配置文件springMvc3-servlet.xml仔细体会一下

return "demo/index";   

}

六、配置tomcat,启动tomcat后,测试访问localhost

http://localhost:8080/codingYun-Demo/ 访问成功,则说明项目搭建成功。

七、框架源码下载

这次的spring mvc框架实例,我为大家无偿共享到我的云盘了,如有需要,请自行下载。

最后补充一句,有任何问题,您都可以在本页评论处留言,我会尽快为您解答。

http://yunpan.cn/Q7cPpVKAqNN4E  提取码 2fde

如果帮到您的话,请为本文点赞、同时分享给您的好友圈。

******************2014年10月10日18:30:10更新*************

coding云的粉丝们,福利来了。spring Mvc 框架实例进阶篇已经为大家提供了哦,包含了coding云的主页、登录功能、验证码功能,而且页面集成了bootstrap前端框架哦,大家快快来下载项目源码吧。