设为首页 收藏本站
查看: 1038|回复: 0

[经验分享] 手把手教你搭建SpringMVC

[复制链接]

尚未签到

发表于 2017-2-25 09:53:26 | 显示全部楼层 |阅读模式
为什么需要Spring MVC
  最开始接触网页的时候,是纯的html/css页面,那个时候还是用Dreamweaver来绘制页面。
  随着网站开发的深入,开始学习servlet开发,记得最痛苦的就是servlet返回网页的内容是字符串拼接的html页面,整不好就无法显示....
  再到后来开学学习SSH,庞大的架构眼花缭乱。Struts繁杂的标签、hibernate搞不清楚的数据表,Spring不知道哪里搞错的bean。
  最后随着发展,前端开始占有一席之地,nodejs风生水起,很多业务逻辑开始前置。再也看不到当初的bo、dao了,取而代之的是各种框架的mvvm,后台减轻压力只负责一些必要的逻辑。
  到现在,好像web开发又发展到了一个阶段——前端由于Nodejs的作用,可以支撑一部分业务逻辑,通过转发代理,统一发往后台。后台通过url实现mvc,对性持久化、更深入的逻辑操作等等。Spring MVC在这里就起了很关键的作用....它通过Url拦截请求,自定义业务逻辑,可以返回自定义的view或者模型数据。
  当然,上面的鬼扯都是片面的,不代表行业的发展,只是博主管中窥豹而已。
  下面步入正题,说说Spring MVC的最小化配置,给入门的朋友引个路。

Spring MVC的最小化配置

需要的jar包


  • Spring framework spring-context
  • Spring framework spring-mvc
  具体可以参考maven中的引用:
  

<dependency>  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.2.4.RELEASE</version>
  
</dependency>
  
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.2.4.RELEASE</version>
  
</dependency>
  

web.xml配置
  

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>  
<web-app  version=&quot;3.1&quot; xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;  
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee
  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd&quot;>
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
  <!-- 默认是/WEB-INF/applicationContext.xml -->
  </context-param>
  <listener>
  <listener-class>
  org.springframework.web.context.ContextLoaderListener
  </listener-class>
  </listener>
  <servlet>
  <servlet-name>SpringMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>
  <!-- 默认是/WEB-INF/[servlet名字]-servlet.xml -->
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>SpringMVC</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>
  

  其中,必要的配置就是指定servlet和listener.


  • ContextLoaderListener指定了IOC容器初始化的方法
  • DispatcherServlet则定义了mvc的相关内容,并配置拦截的url,如上面所示,所有/开头的请求,都会通过SpringMVC这个servlet进行处理。
  他们都需要一个xml文件,默认位置上面已经说过了。

applicationContext.xml
  空的,反正咱也没用什么bean。
  

<beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
  xmlns:context=&quot;http://www.springframework.org/schema/context&quot; xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
  xsi:schemaLocation=&quot;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/aop
  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd&quot;>
  

  
</beans>
  

SpringMVC-servlet.xml
  里面放一个扫描controller的配置即可。
  

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>  
<beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
  xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
  xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
  xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
  xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-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/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/tx
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd&quot;>
  <!-- 设置使用注解的类所在的jar包 -->
  <context:component-scan base-package=&quot;hello&quot; />
  
</beans>
  

controller文件
  

package hello;  

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

  
@Controller

  
public>  @RequestMapping(&quot;/hello&quot;)
  public @ResponseBody String test() {
  return &quot;hello, world! This com from spring!&quot;;
  }
  

  
}
  

总结一下:
  1 两个maven依赖,spring-context;spring-mvc。maven就会自动下载所有关联的jar包,包括


  • spring-webmvc
  • spring-beans
  • spring-core
  • spring-expression
  • spring-web
  • spring-context
  • spring-aop
  • aopalliance
  • commons-logging
  2 一个web.xml文件,配置了listener和servlet
  
3 两个spring相关的文件,applicationContext.xml和servletName-servlet.xml
  
4 一个controller文件,配置了拦截的url处理代码
  有了这些准备工作,运行后输入
  

http://localhost:8080/SpringTest/hello  

  就能得到
  

hello, world! This com from spring!  

  这样的信息,恭喜你的SpringMVC搭起来了!
  最后附送源码的度盘连接
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-346896-1-1.html 上篇帖子: unofficial 专注于学习分享的www.unofficial.cn 下篇帖子: Ubuntu下Vim配置(k-vim)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表