(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

利用Maven 创建Spring+SpringMVC+MyBatis+Oracle 项目

分了三个项目  Dao   (jar)   Service (jar)   Controller (web)

工程下载地址:https://pan.baidu.com/s/1Q9I6V9Mlw47Eua18N9do3Q 密码 1kbs

遇到的几个坑:

1、oracle 的数据库连接jar  Maven库中不存在!!!

Missing artifact com.oracle:ojdbc6 :jar:11.2.0

需要手动下载ojdbc  然后编译安装到本地maven仓库 :Windows(7) 系统运行 cmd 打开 DOS 窗口,进入 Oracle JDBC 驱动包所在的目录,执行命令

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2. -Dpackaging=jar -Dfile=ojdbc6.jar

参考解决此问题的文章地址:Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0 解决方案

如果还报 驱动找不到

Could not
load JDBC driver class [oracle.jdbc.OracleDriver]

则直接把 ojdbc6.jar 直接放到tomcat的lib目录下。

2、*mapper.xml 等配置文件通常情况下要放在  src/main/resources目录下(推荐)!!!

如果*mapper.xml 是放在 src/main/java 目录下 则有时会出现 找不到xml文件的的情况,

for property 'mapperLocations'; nested exception is java.lang.IllegalArgumentException: Could not resolve resource

出现上述错误的主要原因在于 maven 并不会自动将 src/main/java 中的 xml 等资源文佳复制到 target/classes 中去,

,需要在pom.xml中 添加如下信息

 <build>
<resources>
<!-- 编译之后包含xml -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<!-- 编译之后包含xml -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

参考解决此问题的文章地址:maven项目无法打包编辑mapper下的xml文件

以上是利用Spring4+SpringMvc+mybatis+maven+Oracle 分项目搭建框架过程中遇到的坑。

下面贴出搭建过程:

1、dao 项目创建:

在eclipse中 new --->other--->maven ---->maven project 如下图:

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

next 如下图

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

next ,普通Java项目  选择quickstart  标红一项;javaweb项目 选择 webapp标红一项。此处是普通java项目 选择第一项。

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

next之后填写 Group Id   、Artifact Id 、Version(默认即可)   这三个参数组成了 Maven中 定位一个jar的坐标。

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

finish创建的项目,目录结构如下:

src/main/java

src/test/java

src/main/resources

pom.xml

打开pom.xml 注释以下标注内容

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

上述四个文件作用 可参考  (3)Maven快速入门_3在Eclipse中创建Maven项目打包成jar

此处说明下:  main 目录下创建  dao和 entity的 package  对应的存放*Mapper.xml的mappers 的package需要放在resources目录下(推荐)  。如果 mappers的package放在 main下 dao 和 entity同一目录下 ,则 需要注意解决一下上文提到的mapper.xml找不到错误的问题。

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

pom.xml 内容:

log4j  、ojdbc 、mybatis的  maven 插件jar包。注:ojdbc一定要安装到本地maven仓库,否则即使在 pom.xml中引入ojdbc   maven依赖中也没有 ojdbc.jar

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.inspur.smvc</groupId>
<artifactId>dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- 注释
<name>dao</name>
<url>http://maven.apache.org</url>
--> <!-- <build> -->
<!-- <resources> -->
<!-- 编译之后包含xml -->
<!-- <resource> -->
<!-- <directory>src/main/java</directory> -->
<!-- <includes> -->
<!-- <include>**/*.xml</include> -->
<!-- </includes> -->
<!-- <filtering>true</filtering> -->
<!-- </resource> -->
<!-- 编译之后包含xml -->
<!-- <resource> -->
<!-- <directory>src/main/resources</directory> -->
<!-- <includes> -->
<!-- <include>*.xml</include> -->
<!-- </includes> -->
<!-- <filtering>true</filtering> -->
<!-- </resource> -->
<!-- </resources> -->
<!-- </build> --> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies> <!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> <!-- https://mvnrepository.com/artifact/oracle/ojdbc -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version> </dependency> <!-- <dependency> -->
<!-- <groupId>commons-dbcp</groupId> -->
<!-- <artifactId>commons-dbcp</artifactId> -->
<!-- <version>1.2.2</version> -->
<!-- </dependency> --> <!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- junit4.12 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

maven repository  maven仓库中搜索需要的jar  如 搜索 mybatis    把红色框选的内容复制到 pom.xml即可

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

UserDao.java

package com.inspur.smvc.dao;

import com.inspur.smvc.entity.User;

public interface UserDao {
public User login(User user);
}

User.java

package com.inspur.smvc.entity;

public class User {

    private int id;

    private String userName;
private String password; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.inspur.smvc.dao.UserDao">

<select id="login" parameterType="User" resultType="User">
select * from t_user where userName=#{userName} and password=#{password}
</select>

</mapper>

log4j.properties

log4j.rootLogger=DEBUG, Console  

#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 别名 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<package name="com.inspur.smvc.entity"/>
</typeAliases>
</configuration>

t_user表的sql语句

-- Create table
create table T_USER
(
id NUMBER,
username NVARCHAR2(100),
password NVARCHAR2(100)
)

右键点击 pom.xml  ---- Run As---- maven install  在target和本地仓库中生成  jar    target目录中,生成内容如下:

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

右键点击 pom.xml  ---- Run As---- maven clean   在target和本地仓库中清理 已经生成  jar ,重新执行 maven  install  可再次生成jar

2、创建 service 项目  与dao项目创建的方式相同 ,只贴代码

项目结构如下:

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

pom.xml文件内容:

spring4 的maven依赖包, spring4 与mybatis集成的的maven依赖包 。

注:依赖dao项目 需要添加 dao项目的 maven信息 , 可从dao项目 pom.xml的中复制 groupid  artifactId version  信息。

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.inspur.smvc</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <!-- <name>service</name> -->
<!-- <url>http://maven.apache.org</url> --> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies> <!-- 依赖 dao -->
<dependency>
<groupId>com.inspur.smvc</groupId>
<artifactId>dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency> <!-- spring start-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency> <!-- spring end --> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

UserServiceImpl.java

package com.inspur.smvc.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.inspur.smvc.dao.UserDao;
import com.inspur.smvc.entity.User;
import com.inspur.smvc.service.UserService; @Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao; public User login(User user){
return userDao.login(user);
} }

UserService.java

package com.inspur.smvc.service;

import com.inspur.smvc.entity.User;

public interface UserService {
public User login(User user);
}

applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 自动扫描 -->
<context:component-scan base-package="com.inspur.smvc.service" /> <!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@10.110.2.88:1521:scossora"/>
<property name="username" value="costnew"/>
<property name="password" value="costnew"/>
</bean> <!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mappers.xml文件 -->
<property name="mapperLocations" value="classpath:com/inspur/smvc/mappers/*.xml"></property>
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.inspur.smvc.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 配置事务通知属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义事务传播属性 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="new*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="check*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice> <!-- 配置事务切面 -->
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* com.inspur.smvc.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config> </beans>

依然需要 点击 pom.xml 右键执行 Run As  执行 maven  install 在target目录 和本地maven仓库生成 jar

3、controller 项目 (web)

此处需要创建maven版本的 web项目 :选择 -webapp

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

项目结构如下:

此处比maven普通的java项目需要多web项目结构的信息 都存放在src/main/webapp下

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

pom.xml文件:

jsp 、servlet 、jstl、Springmvc 的 maven插件支持   还需要引入 service项目 (service项目中已经引入dao了, 所以不需要单独引入dao)。

引入之后maven插件中就会有service和dao项目

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

<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>com.inspur.smvc</groupId>
<artifactId>controller</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version> <!-- <name>controller Maven Webapp</name> -->
<!-- <url>http://maven.apache.org</url> --> <dependencies> <!-- 引入service -->
<dependency>
<groupId>com.inspur.smvc</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency> <!-- 添加Spring支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>controller</finalName>
</build>
</project>

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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 使用注解的包,包括子集 -->
<context:component-scan base-package="com.inspur.smvc.controller" /> <!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp"></property>
</bean> </beans>

UserController.java

package com.inspur.smvc.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.inspur.smvc.entity.User;
import com.inspur.smvc.service.UserService; @Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService; @RequestMapping("/login")
public String login(User user,HttpServletRequest request){
User resultU = userService.login(user);
if(null == resultU){
request.setAttribute("user", user);
request.setAttribute("errorMsg", "用户名或者密码错误!");
return "index";
}else{
request.getSession().setAttribute("currentUser", resultU);
return "redirect:/success.jsp";
}
} }

MANIFEST.MF

Manifest-Version: 1.0
Class-Path:

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MyBatisPro05</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 添加对springmvc的支持 -->
<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.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

index.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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/user/login.do" method="post">
userName:<input type="text" name="userName" value="${user.userName }"/><br/>
password:<input type="password" name="password" value="${user.password }"><br/>
<input type="submit" value="login"/><font color="red">${errorMsg }</font>
</form>
</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>Insert title here</title>
</head>
<body>
欢迎:${currentUser.userName }
</body>
</html>

controller web项目直接右键项目名 --->Run As --->Maven install 创建到target 和本地maven仓库中。

右键选中项目  ---Run As --->Run on Server 发布到tomcat

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

输入错误的用户名或者密码 提示如下:

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

输入正确的用户名或者密码 提示如下:

(4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中

至此 : Spring4+mybatis3+SpringMvc+Oracle11g+tomcat7+maven 版本的工程就创建完成了。

上一篇:搭建Spring + SpringMVC + Mybatis框架之三(整合Spring、Mybatis和Spring MVC)


下一篇:用十条命令在一分钟内检查Linux服务器性能[转]