当前位置: 移动技术网 > IT编程>开发语言>Java > 手把手教你搭建SpringMVC框架——最小化配置

手把手教你搭建SpringMVC框架——最小化配置

2019年07月22日  | 移动技术网IT编程  | 我要评论

为什么需要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="1.0" encoding="utf-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
            xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
            xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee 
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    
   <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="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  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/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">

</beans>

springmvc-servlet.xml

里面放一个扫描controller的配置即可。

<?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:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemalocation="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">
  <!-- 设置使用注解的类所在的jar包 -->
  <context:component-scan base-package="hello" />
</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 class hellocontroller {
  
  @requestmapping("/hello")
  public @responsebody string test() {
    return "hello, world! this com from spring!";
  }

}

总结一下:

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搭起来了!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网