本文说明:本文介绍使用maven搭建SpringMVC最简单的框架程序过程,适合初学者上手。
下载链接 点此进入下载链接
1.创建一个maven webapp工程。
2.修改WEB-INF目录下的web.xml文件:
内容如下,文中有注释
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- version="3.0">
- <display-name>Archetype Created Web Application</display-name>
- <!-- spring MVC的核心就是DispatcherServlet,使用springMVC的第一步就是将下面的servlet放入web.xml
- servlet-name属性非常重要,默认情况下,DispatchServlet会加载这个名字-servlet.xml的文件,如下,就会加载
- dispather-servlet.xml,也是在WEN-INF目录下。
- -->
- <servlet>
- <servlet-name>dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <!-- 设置dispatchservlet的匹配模式,通过把dispatchservlet映射到/,默认servlet会处理所有的请求,包括静态资源 -->
- <servlet-mapping>
- <servlet-name>dispatcher</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <!-- 字符集过滤器 -->
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
3.在WEN-INF目录下创建dispatcher-servlet.xml
内容如下,文中有注释。
- <?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:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:p="http://www.springframework.org/schema/p"
- 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"
- default-lazy-init="true">
- <!-- 通过mvc:resources设置静态资源,这样servlet就会处理这些静态资源,而不通过控制器 -->
- <!-- 设置不过滤内容,比如:css,jquery,img 等资源文件 -->
- <mvc:resources location="/*.html" mapping="/**.html" />
- <mvc:resources location="/css/*" mapping="/css/**" />
- <mvc:resources location="/js/*" mapping="/js/**" />
- <mvc:resources location="/images/*" mapping="/images/**" />
- <!-- 添加注解驱动 -->
- <mvc:annotation-driven />
- <!-- 默认扫描的包路径 -->
- <context:component-scan base-package="com.my.web" />
- <!-- mvc:view-controller可以在不需要Controller处理request的情况,转向到设置的View -->
- <!-- 像下面这样设置,如果请求为/,则不通过controller,而直接解析为/index.jsp -->
- <mvc:view-controller path="/" view-name="index" />
- <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
- <!-- 配置jsp路径前缀 -->
- <property name="prefix" value="/"></property>
- <!-- 配置URl后缀 -->
- <property name="suffix" value=".jsp"></property>
- </bean>
- </beans>
<context:component-scan base-package="com.my.web" />中的路径,com.my.web,是需要在src/main/java中创建的包,用来放Java代码。
4.使用maven引入SpringMVC所依赖的jar包。
修改pom.xml文件
4.1添加属性,在<project>标签中
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <spring.version>3.1.2.RELEASE</spring.version>
- </properties>
- <!-- spring begin -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!-- spring end -->
做完上面的准备工作之后就可以写代码了。
5.写前台页面:
在maven工程中生成的有一个index.jsp,将其修改成一下内容,很简单,只是一个登陆框,提交一个form表单,中的用户名和密码。
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>欢迎</title>
- </head>
- <body>
- <h2>Hello World!</h2>
- <form action="login">
- 用户名:<input id="username" name="username" type="text"></input><br>
- 密 码:<input id="username" name="password" type="password"></input><br>
- <input type="submit">
- </form>
- <span>当前IP:<%=request.getRemoteAddr() %></span>
- </body>
- </html>
- package com.my.web.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- /**
- * TODO 控制层代码
- * @author 591791
- * @date 2014年11月27日
- */
- @Controller
- public class MyController {
- @RequestMapping("login") //用来处理前台的login请求
- private @ResponseBody String hello(
- @RequestParam(value = "username", required = false)String username,
- @RequestParam(value = "password", required = false)String password
- ){
- return "Hello "+username+",Your password is: "+password;
- }
- }