本项目旨在搭建一个简单的Spring MVC框架,了解Spring MVC的基础配置等内容。

一、项目结构

本项目使用idea intellij创建,配合maven管理。整体的目录结构如图:
其中java文件夹是sources文件夹,resources是资源文件夹。spring文件夹里是Spring上下文配置和Spring MVC配置文件。
 
需要注意的是,项目自动生成以后会有两个web文件目录,一个是web文件夹(我这里已经删除了),另一个是默认的webapp文件夹。我这里使用默认的目录,也就是说webapp文件夹。页面文件都放在该目录里面。
                                                                                         *如图可配置默认的web文件默认路径。*
二、配置文件
maven配置,添加各种依赖
省略,这个不是重点。需要注意的是,添加依赖的时候不能重复添加不同版本的包。
web.xml文件配置
首先,添加spring 上下文配置,指定Spring Context由classpath下的spring文件夹中的app-context.xml提供:
  1. <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/app-context.xml</param-value>
    </context-param>

    然后添加Spring监听器:

  1. <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    接下来配置Spring MVC的dispatherservlet,同时配置该servlet要拦截的URL。

  1. <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/mvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 配置要拦截的URL -->
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    最后,配置一个welcom-file-list。

web.xml全部的代码如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
    <web-appxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.4"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <!-- spring context 配置文件 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/app-context.xml</param-value>
    </context-param>
    <!-- spring 监听器配置 -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <!--spring 防内存溢出监听器 -->
    <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <!-- spring mvc servlet配置文件 -->
    <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/mvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 配置要拦截的URL -->
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
    <welcome-file></welcome-file>
    </welcome-file-list>
    </web-app>

    配置Spring MVC文件

这里主要配置自动注解、mvc资源引用、视图解析器等
代码如下:
  1. <?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <mvc:resources location="/js/" mapping="/js/**"/> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    <mvc:annotation-driven/>
    <context:annotation-config/>
    <mvc:default-servlet-handler/> <!--添加component扫描,使package下面的注解生效 -->
    <context:component-scan base-package="com.wxspringmvc.controller"/> <!--添加页面视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/page/"/>
    <property name="suffix" value=".jsp"/>
    <property name="contentType" value="text/html;charset=UTF-8"/>
    </bean>
    </beans>

    配置applicationContext.xml

这里主要是提供默认的上下文,不需要额外的配置,直接使用生成的文件就可以。代码就省略了。
要注意的是,applicationContext.xml是一定要配置的。在web.xml文件中如果不配置,系统会自动到WEB-INF目录下寻找。
View和Controller
这里使用一个简单的用户登录,完成后在页面显示用户名和密码的流程来展示。
View:index.jsp
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>请登录</title>
    </head>
    <body>
    <h5>this is index.jsp</h5>
    <form action="/user/index" method="post">
    <p>用户名:</p><input type="text" id="username" name="username">
    <p>密码:</p><input type="password" id="password" name="password">
    <p><input type="submit" value="提交"></p>
    </form>
    </body>
    </html>

    表单使用post的方式提交到/user/index路径。

Controller:UserController.java
  1. @Controller
    @RequestMapping(value = "/user")
    public class UserController {
    @RequestMapping(value = "/index" ,method= RequestMethod.POST)
    public ModelAndView userIndex(String username,String password){
    ModelAndView mav = new ModelAndView("user/success"); mav.addObject("username",username);
    mav.addObject("password",password);
    return mav;
    } }

    这里可以添加一个简单的校验,如果用户名和密码有一个为空,则不能提交:

修改UserController.java的代码如下:
  1. @Controller
    @RequestMapping(value = "/user")
    public class UserController {
    @RequestMapping(value = "/index" ,method= RequestMethod.POST)
    public ModelAndView userIndex(String username,String password){
    ModelAndView mav = new ModelAndView("user/success");
    if(!matchParams( username, password)){
    return new ModelAndView("/index");
    }
    mav.addObject("username",username);
    mav.addObject("password",password);
    return mav;
    } private boolean matchParams(String username,String password){
    if(isEmpty(username)||isEmpty(password))
    return false;
    else
    return true;
    } private boolean isEmpty(String s){
    if(s==null || "".equals(s))
    return true;
    else
    return false;
    }
    }

    View:登录成功界面:success.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>用户首页</title>
    </head>
    <body>
    <p>用户名:${username}</p>
    <p>密码:${password}</p>
    </body>
    </html>

    效果展示

默认页:
输入不正确,提交结果:
输入正确提交:
 
----------------------------------------------------------------------------------------------------------------------------
 
以上就是一个简单的Spring MVC演示了。

Spring MVC篇一、搭建Spring MVC框架的更多相关文章

  1. Spring第一篇【介绍Spring、引入Spring、Spring六大模块】

    前言 前面已经学习了Struts2和Hibernate框架了.接下来学习的是Spring框架-本博文主要是引入Spring框架- Spring介绍 Spring诞生: 创建Spring的目的就是用来替 ...

  2. [Java,MVC] Eclipse下搭建Spring MVC

    转自:http://blog.csdn.net/blue_jjw/article/details/8752466 一.新建Dynamic Web Project 一个web工程最基本的,只看3个地方, ...

  3. 【译文】用Spring Cloud和Docker搭建微服务平台

    by Kenny Bastani Sunday, July 12, 2015 转自:http://www.kennybastani.com/2015/07/spring-cloud-docker-mi ...

  4. Spring Cloud和Docker搭建微服务平台

    用Spring Cloud和Docker搭建微服务平台 This blog series will introduce you to some of the foundational concepts ...

  5. Spring入门(一):创建Spring项目

    本篇博客作为Spring入门系列的第一篇博客,不会讲解什么是Spring以及Spring的发展史这些太理论的东西,主要讲解下如何使用IntelliJ IDEA创建第一个Spring项目以及通过一个示例 ...

  6. 从零开始学 Java - 搭建 Spring MVC 框架

    没有什么比一个时代的没落更令人伤感的了 整个社会和人都在追求创新.进步.成长,没有人愿意停步不前,一个个老事物慢慢从我们生活中消失掉真的令人那么伤感么?或者说被取代?我想有些是的,但有些东西其实并不是 ...

  7. 搭建spring mvc项目

    在之前搭建maven项目这篇的基础上继续集成,引入spring mvc支持 一.添加jar包引用 修改pom.xml文件,加入:(其他关联的jar包maven会自动引用) <!-- 项目属性 - ...

  8. mac os版本Intellij IDEA 搭建spring mvc的maven工程(新手教学)

    由于近期换了新公司,又换mac pro作为新电脑,打算把用了很多年的eclipse换成IDEA(IDEA比eclipse的好处我就不多说了),由于mac os和IDEA刚开始用不久,所以专门用一篇博客 ...

  9. 零基础搭建 spring mvc 4 项目(本文基于 Servlet 3.0)

    作者各必备工具的版本如下: Tomcat:apache-tomcat-7.0.63 (下载链接) Java EE - Eclipse:Luna Service Release 1 v4.4.1 (下载 ...

随机推荐

  1. 自定义TextView 调用ttf格式字体

    自定义TextView 调用ttf格式字体 1.<strong>将ttf格式文件存放在assets/fonts/下</strong> 注:PC系统字体存放在C:\Windows ...

  2. iOS 对象的内存管理细节

    通过类创建对象 1.创建对象时,开辟存储空间,通过new方法创建的对象会在 堆 内存中开辟一块存储空间 2初始化所有属性都在堆内存中完成 3.返回值真地址,指针在栈内存中,指针指向的地址是堆里创建对象 ...

  3. C#网络编程数据传输中封装数据帧头的方法

    在C/S端编程的时候,经常要在C端和S端之间传数据时自定义一下报文的帧头,如果是在C/C++,封装帧头是一件很简单的事情,直接把unsigned char *强转为struct就行,但是在C#中,并没 ...

  4. @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...

  5. MVC 添加Area

    在MVC项目中经常会使用到Area来分开不同的模块让项目结构更加的清晰. 步骤如下: 项目 –> 添加 -> 区域 ( Area ) 输入 Admin 添加成功后 Area包含: 创建一个 ...

  6. SpringMVC后台接收list类型的数据的实现方式

    一.背景 最近在做一些东西的时候,遇到一个需要Springmvc后台接收list类型数据的需求,几经辗转才完美解决了这个问题,今天记下来方便以后使用,也分享给需要的小伙伴们~ 二.实现方式 1.实现方 ...

  7. there's no qt version assigned to this project for platform

    VS+Qt编译一个新建的项目报there's no qt version assigned to this project for platform xxx的错误. 解决方案: 打开Qt_vs_add ...

  8. jQuery简单实现iframe的高度根据页面内容自适应的方法(转)

    本文实例讲述了jQuery简单实现iframe的高度根据页面内容自适应的方法.分享给大家供大家参考,具体如下: 方式1: //注意:下面的代码是放在和iframe同一个页面中调用 $("#i ...

  9. Duilib源码分析(一)整体框架

    Duilib界面库是一款由杭州月牙儿网络技术有限公司开发的界面开源库,以viksoe项目下的UiLib库的基础上开发(此后也将对UiLib库进行源码分析):通过XML布局界面,将用户界面和处理逻辑彻底 ...

  10. Launching web on MyEclipse Tomcat 问题

    错误提示: Launching web on MyEclipse Tomcat has encountered a problemAn internal error occurred during: ...