搭建SpringMVC——最小化配置

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

时间: 2024-07-30 06:12:49

搭建SpringMVC——最小化配置的相关文章

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

为什么需要Spring MVC 最开始接触网页的时候,是纯的html/css页面,那个时候还是用Dreamweaver来绘制页面. 随着网站开发的深入,开始学习servlet开发,记得最痛苦的就是servlet返回网页的内容是字符串拼接的html页面,整不好就无法显示.... 再到后来开学学习SSH,庞大的架构眼花缭乱.Struts繁杂的标签.hibernate搞不清楚的数据表,Spring不知道哪里搞错的bean. 最后随着发展,前端开始占有一席之地,nodejs风生水起,很多业务逻辑开始前置

Git.之.最小化配置

Git.之.最小化配置 做一个全局的用户配置,便于以后提交代码等,记录当前操作的用户. ## 添加配置 # git config [--local | --global | --system] user.name 'Your name' # git config [--local | --global | --system] user.email 'Your email' ## 查看配置 # git config --list [--local | --global | --system] ##

Bean自动装配-XML最小化配置

上一个讲了怎样用xml配置所有的Bean以及进行依赖注入,但是这样太麻烦了,对于每一个bean都要写一个xml,可以有几种方式进行自动装配. 四种装配方式 byName(通过名字装配,这时属性的名字必须与bean名字一致) byType(通过类型,匹配与interface或class相同的类型),这种是找到对应的方法,然后进行setter方法进行注入的 constructor(也是通过类型匹配,但是是通过new的进行装配的) 最佳自动装配(先用constructor装配,然后再用byType)

linux最小化原则

安装系统最小化 开启程序服务最小化 操作最小化 登录最小化(平时无需求不用root登录) 权限最小化 配置参数合理

linux(6.8版本最小化安装)安装nginx实战

1.安装pcre: 查看是否安装过pcre: [[email protected] ~]# rpm -qa pcrepcre-devel pcre-7.8-6.el6.x86_64 若没有则执行安装命令:[[email protected] ~]# yum install pcre-devel -y [[email protected] ~]# rpm -qa pcrepcre-devel pcre-7.8-7.el6.x86_64 pcre-devel-7.8-7.el6.x86_64 2.安

springmvc基于java配置的实现

该案例的github地址:https://github.com/zhouyanger/demo/tree/master/springmvc-noxml-demo 1.介绍 之前搭建SpringMvc项目要配置一系列的配置文件,比如web.xml,applicationContext.xml,dispatcher.xml.Spring 3.X之后推出了基于JavaConfig方式以及注解的形式的配置.在一定程度上简化了Spring项目的配置.近几年特别火的SpringBoot,大大的简化了创建项目

UBUNTU最小化搭建LXDE桌面环境

1.基础系统搭建: sudo apt-get install xorg lxde-common lxsession desktop-file-utils openbox sudo apt-get install build-essential leafpad lxterminal obconf sudo apt-get install xarchiver unrar rar p7zip-full sudo apt-get install ttf-wqy-microhei 2.安装小小输入法: 访

搭建LNMP环境(基于最小化安装CentOS 6.5)

本文档主要说明在单台服务器上手动安装LNMP环境的操作步骤,本文档使用的系统版本可能与您的实际使用版本不同,您可以根据实际情况选择相应版本. 一.本文档LNMP环境版本说明: OS:最小化安装CentOS 6.5 Nginx:nginx-1.10.2.tar.gz MySQL:mysql-5.6.24.tar.gz PHP:php-5.6.23.tar.bz2 二.搭建LNMP环境基本步骤 1. 准备编译环境 2. 安装nginx 3. 安装mysql 4. 安装php-fpm 5. 测试访问

最最最小化的spring配置

这个配置只能用来使用最基本的spring bean的使用. 只需要这些包(MAVEN的POM.XML文件): <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>4.0.0.RELEASE</version>