创建一个maven项目。
在pom文件中配置用到的jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springMVCdemo</groupId>
<artifactId>springMVC</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>springMVC</finalName>
</build>
</project>
1、在web.xml中配置ContextLoaderListener、applicationContext.xml、DispatcherServlet。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<!-- 使系统在启动时装在servlet而不是第一个servlet被访问 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
2、在WEB-INF下创建spring-servlet.xml配置文件,这里我把文件名改成了spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.xjq"></context:component-scan>
</beans>
3、在WEB-INF下创建applicationContext.xml配置文件。因为这里只做一个springMVC的简单demo,涉及到数据源的配置文件都注释掉。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 -->
<context:component-scan base-package="com.xjq"></context:component-scan>
<!-- 引入jdbc配置文件 -->
<!-- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> -->
<!-- <property name="locations"> -->
<!-- <list> -->
<!-- <value>classpath*:jdbc.properties</value> -->
<!-- </list> -->
<!-- </property> -->
<!-- </bean> -->
<!-- dataSource 配置 -->
<!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> -->
<!-- 基本属性 url、user、password -->
<!-- <property name="url" value="${jdbc.url}" /> -->
<!-- <property name="username" value="${jdbc.username}" /> -->
<!-- <property name="password" value="${jdbc.password}" /> -->
<!-- 配置初始化大小、最小、最大 -->
<!-- <property name="initialSize" value="1" /> -->
<!-- <property name="minIdle" value="1" /> -->
<!-- <property name="maxActive" value="20" /> -->
<!-- 配置获取连接等待超时的时间 -->
<!-- <property name="maxWait" value="60000" /> -->
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<!-- <property name="timeBetweenEvictionRunsMillis" value="60000" /> -->
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<!-- <property name="minEvictableIdleTimeMillis" value="300000" /> -->
<!-- <property name="validationQuery" value="SELECT 'x'" /> -->
<!-- <property name="testWhileIdle" value="true" /> -->
<!-- <property name="testOnBorrow" value="false" /> -->
<!-- <property name="testOnReturn" value="false" /> -->
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<!-- <property name="poolPreparedStatements" value="false" /> -->
<!-- <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->
<!-- 配置监控统计拦截的filters -->
<!-- <property name="filters" value="stat" /> -->
<!-- </bean> -->
<!-- mybatis文件配置,扫描所有mapper文件 -->
<!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml" p:mapperLocations="classpath:com/eduoinfo/finances/bank/web/dao/*.xml" /> -->
<!-- spring与mybatis整合配置,扫描所有dao -->
<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.eduoinfo.finances.bank.web.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory" /> -->
<!-- 对dataSource 数据源进行事务管理 -->
<!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" /> -->
<!-- 配置使Spring采用CGLIB代理 -->
<!-- <aop:aspectj-autoproxy proxy-target-class="true" /> -->
<!-- 启用对事务注解的支持 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
<!-- Cache配置 -->
<!-- <cache:annotation-driven cache-manager="cacheManager" /> -->
<!-- <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" /> -->
<!-- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManagerFactory" /> -->
</beans>
4、在WEB-INF下创建jsp文件夹,在文件夹中创建login.jsp和success.jsp。
login.jsp:
<%@ 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>
<form action="login.html">
<p>
用户名:<input name="uname" type="text">
</p>
<p>
密 码:<input name="psw" type="password">
</p>
<p>
<input name="submit" type="submit">
</p>
</form>
<font color="red">${error}</font>
</body>
</html>
success.jsp:
<%@ 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>
<h1 >登陆成功</h1>
</body>
</html>
5、创建controller和service的java类。
controller.java:
package com.xjq.springmvc.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.xjq.springmvc.service.LoginService;
@Controller
public class LoginController {
// 登陆页面url
public static final String loginUrl = "WEB-INF/jsp/login.jsp";
// homePage url
public static final String homeUrl = "WEB-INF/jsp/success.jsp";
@Resource
LoginService service;
@Resource
HttpServletRequest request;
@RequestMapping("spring")
public ModelAndView toLoginPage(){
return new ModelAndView(loginUrl);
}
@RequestMapping("login")
public ModelAndView doLogin(){
String username = request.getParameter("uname");
String pwd = request.getParameter("psw");
return service.doLogin(loginUrl, homeUrl, username, pwd);
}
}
service.java:
package com.xjq.springmvc.service;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
@Service
public class LoginService {
public ModelAndView doLogin(String loginUrl, String homeUrl, String username, String pwd){
if(username == null || "".equals(username)){
return new ModelAndView(loginUrl, "error", "用户名不能为空!");
}
if(pwd == null || "".equals(pwd)){
return new ModelAndView(loginUrl, "error", "密码不能为空!");
}
if(!"admin".equals(username) || !"123".equals(pwd)){
return new ModelAndView(loginUrl, "error", "用户名或密码不正确!");
}
return new ModelAndView(homeUrl);
}
}
至此,代码和配置结束,运行tomcat。