项目结构图
jar包
springmvc-anno.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 加上此标签,getmapping和postmapping等新注解就可使用 -->
<mvc:annotation-driven />
<!-- 指定需要扫描的包 -->
<context:component-scan base-package="com.controller" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 定义视图解析器 -->
<bean id="viewResolver" class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 设置前缀 -->
<property name="prefix" value="/" />
<!-- 设置后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
jdbc.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库驱动 -->
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<!-- 连接字符串 -->
<property name="url"
value="jdbc:mysql://localhost:3306/emis" />
<!-- 数据库用户名和密码 -->
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 配置jdbc模板 -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<!-- ref引用上面的数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 声明事务管理器,依赖于数据源 -->
<bean
id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 基于annotation(注解)的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- id:唯一标识
transaction-manager属性用于指定事务管理器
-->
<!-- 基于xml的声明式事务 -->
<tx:advice id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<!-- *表示任意方法 -->
<tx:method name="*"
propagation="REQUIRED"
isolation="DEFAULT"
read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 声明式事务是建立在aop上的 -->
<!--编写aop,自动对目标生成代理 ,aspectj框架-->
<aop:config>
<aop:pointcut expression="execution(* com.controller.*.*(..))"
id="txPointCut" />
<!-- 切面 将切入点与通知整合 -->
<aop:advisor advice-ref="txAdvice"
pointcut-ref="txPointCut" />
</aop:config>
</beans>
controller类
package com.controller;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.user.User;
@Controller
public class LoginController {
JdbcTemplate jdbcTemplate;
{
// 加载配置文件
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("jdbc.xml");
//获取JdbcTemplate实例
jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
}
//被标注的方法成为请求方法,收到url时被调用
@RequestMapping("/toLoginView")
private String toLoginView() {
return "login";
}
//@RequestMapping(value="/login",method = RequestMethod.POST)
//处理post请求
@PostMapping(value="/register")
public String register(HttpServletRequest request, HttpServletResponse response)
{
String users = request.getParameter("user");
String pass = request.getParameter("pass");
if(users==null || users.length()==0 || pass==null || pass.length()==0)
{
request.setAttribute("errorMsg", "请输入用户名和密码的参数!");
return toLoginView();
}
//若要插入的字段和表里的字段不匹配时,必须在表后面指定列名,字符型的值要用单引号引起来
//否则,不必用单引号
jdbcTemplate.execute("insert into username(user,password) values("+users+","+pass+")");
//返回视图
return "";
}
@PostMapping(value="/login")
public String login(HttpServletRequest request, HttpServletResponse response)
{
String user = request.getParameter("user");
String pass = request.getParameter("pass");
if(user==null || user.length()==0 || pass==null || pass.length()==0)
{
request.setAttribute("errorMsg", "请输入用户名和密码的参数!");
return toLoginView();
}
//BeanPropertyRowMapper
//常应用于使用Spring的JdbcTemplate查询数据库
//获取List结果列表,数据库表字段和实体类自动对应
RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
List<User> list = jdbcTemplate.query("select * from username where user='"+user+"'and password='"+pass+"'",rowMapper);
if(list.size()>0) //返回的list里有数据
{
request.getSession().setAttribute("user", user);
//返回视图
return "redirect:/index.jsp";
}
else
{
request.setAttribute("errorMsg", "用户名或密码错误!");
return toLoginView();
}
}
}
持久化类
package com.user;
public class User { //持久化类
private String userName; //属性名对应表里的列名
private String passWord;
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;
}
@Override
public String toString() {
return "User [userName=" + userName + ", passWord=" + passWord + "]";
}
}
login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>login</title>
</head>
<body>
<center>
<h3>用户登录</h3>
<!-- controller类里的login方法上的postmapping注解 -->
<form action="login" method="post">
<input type="text" name="user" style="width:200px" /><br>
<input type="password" name="pass" style="width:200px" /><br><br>
<input type="submit" value="登 录" /> <br>
<font color="red"><%=request.getAttribute("errorMsg")==null?"":request.getAttribute("errorMsg") %> </font>
</form>
</center>
</body>
</html>
register.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>register</title>
</head>
<body>
<center>
<h3>用户注册</h3>
<!-- controller类里的register方法上的postmapping注解 -->
<form action="register" method="post">
<input type="text" name="user" style="width:200px" /><br>
<input type="password" name="pass" style="width:200px" /><br><br>
<input type="submit" value="注 册" /> <br>
<font color="red"><%=request.getAttribute("errorMsg")==null?"":request.getAttribute("errorMsg") %> </font>
</form>
</center>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>login</title>
</head>
<body>
欢迎用户,<%=session.getAttribute("user") %>
</body>
</html>