1,所需的包
2,配置文件,applicationContext放在src目錄下,web.xml和springMVC-servlet.xml放在WEB-INF目錄下
1)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"> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <display-name>ssm</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <!-- servlet的名字決定了springMVC配置文件的名字,springMVC配置文件的名字應爲servlet-name+"-servlet.xml" --> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>3</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
2)springMVC-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 掃描web包,應用Spring的註解 -->
<context:component-scan base-package="com" />
<!-- 配置視圖解析器,將ModelAndView及字符串解析爲具體的頁面 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/"
p:suffix=".jsp" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<!-- 這是攔截器,如果不需要攔截器則註釋掉 juong增加 -->
<!--<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean id="loginInterceptor" class="com.yq.util.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>-->
</beans>
3)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: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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 掃描類包,將標註Spring註解的類自動轉化Bean,同時完成Bean的注入 -->
<context:component-scan base-package="com.*.dao" />
<context:component-scan base-package="com.*.service" />
<!-- 配置數據源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/ssm"
p:username="root"
p:password="root">
<property name="validationQuery">
<value>SELECT 1</value>
</property>
<property name="testOnBorrow">
<value>true</value>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/*/dao/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.*.dao" />
</bean>
<!-- 配置事務管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
<!-- 通過AOP配置提供事務增強,讓service包下所有Bean的所有方法擁有事務 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod"
expression="execution(* com.*.service..*(..)))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
</beans>
3,項目結構
4,java代碼
1)UserCtrl.java:
package com.hx.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.hx.entity.UserEntity;
import com.hx.service.UserService;
@Controller
@RequestMapping("/wap")
public class UserCtrl {
@Autowired
private UserService service;
@RequestMapping("/login")
public String login(){
return "/wap/login";
}
@RequestMapping("/userCenter")
public ModelAndView doLogin(String userName,String password){
System.out.println(userName);
ModelAndView m =new ModelAndView("/wap/index");
Map<String,String> map =new HashMap<String,String>();
map.put("userName", userName);
map.put("password", password);
UserEntity user = service.doLogin(map);
if(user!=null){
m.addObject("userName", user.getUser_name());
}
return m;
}
}
2)UserService.java:
package com.hx.service;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hx.dao.UserDao;
import com.hx.entity.UserEntity;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public UserEntity doLogin(Map<String,String> map){
return userDao.findUser(map);
}
}
3)UserEntity.java:
package com.hx.entity;
public class UserEntity {
private Integer id;
private String user_name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4)UserDao.java:
package com.hx.dao;
import java.util.Map;
import com.hx.entity.UserEntity;
public interface UserDao {
public UserEntity findUser(Map<String,String> map);
}
UserDao.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.hx.dao.UserDao">
<select id="findUser" parameterType="java.util.Map" resultType="com.hx.entity.UserEntity">
select * from user where user_name =#{userName}
<if test="password!='' and password!=null">
and password =#{password}
</if>
</select>
</mapper>
5,jsp代碼
1)login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
<head>
<!-- 定義當前頁面的路徑 -->
<base href="<%=basePath%>wap/">
<title>簡單登錄頁面</title>
</head>
<body>
<form action="userCenter.html" method="POST">
<input name="userName" type="text"/>
<br/>
<input name="password" type="password"/>
<br/>
<input value="提交" type="submit"/>
<input value="重置" type="reset"/>
</form>
</body>
</html>
2)index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>wap/">
<title>簡單首頁</title>
</head>
<body>
${userName}...你好,歡迎登錄。
</body>
</html>
完成上面所有的代碼後,在mysql裏面建一個名爲ssm的庫,在庫裏面建一張user表,添加三個字段id,user_name和password,再添加一條測試數據。然後將項目部署到tomcat,訪問http://localhost/ssm/wap/login.html(端口要設置爲80,否則要加端口號)訪問測試。