重构了一下之前的SSM登陆注册案例,SSM的三大核心IOC,AOP,DI不用多说,网上随便翻一下都有 ,本篇直接上手,使用Maven工程搭建一个简单的SSM框架实现简单的登陆注册,验证重名功能,适合刚上手的朋友看看css
数据库就不贴了,俩字段,username和userpwd,指定username为主键html
<?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" version="2.5"> <display-name>LogAndReg</display-name> <!-- DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加载spring核配置文件及springmvc配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置编码过滤器 ps:tomcat8.0及之后的版本中,对于get请求所提交的参数会进行自动编码处理,而对于post请求没有进行编码处理--> <filter> <filter-name>CharacterEncodingFilter</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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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"> <!-- 放行静态资源 --> <mvc:default-servlet-handler /> <!-- 注解驱动,识别注解 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 扫描Controller层 --> <context:component-scan base-package="cn.gingost.controller"></context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> </beans>
<?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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 加载jdbc.properties配置文件 --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 扫描文件,使注解生效,注意,要先扫描service层 --> <context:component-scan base-package="cn.gingost.service"></context:component-scan> <!-- 配置数据源,用的是阿里的druid链接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${user}"></property> <property name="password" value="${pwd}"></property> <!-- 初始化链接数 --> <property name="initialSize" value="${initialSize}"></property> <!-- 最大链接数 --> <property name="maxActive" value="${maxActive}"></property> <!-- 最小链接数 --> <property name="minIdle" value="${minIdle}"></property> </bean> <!-- 配置mybatis工厂,指定数据源 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 使用mapperLocations的方法指定mapper.xml映射文件位置,此方法不须要配置mybatis-config --> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property> </bean> <!-- Mapper接口扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.gingost.dao"></property> </bean> <!-- 添加事务,通常都建议写上,可是本例过于简单,也能够不写--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 开启事务注解,同上 --> <tx:annotation-driven transaction-manager="txManager" /> </beans>
package cn.gingost.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; /* * 页面跳转方法 */ @Controller public class PagesController { @RequestMapping("/{page}") public String pageTo(@PathVariable String page) { return page; } }
package cn.gingost.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import cn.gingost.pojo.User; import cn.gingost.service.UserService; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/golog") public String log(User user, Model model) { User u = userService.findOne(user); model.addAttribute("user", u); if (u != null) { return "logok"; } else return "logno"; } @RequestMapping("/goreg") public String reg(User user) { User u = userService.checkReg(user.getUsername()); if (u == null) { userService.addOne(user); return "regok"; } return "regno"; } }
package cn.gingost.dao; import cn.gingost.pojo.User; public interface UserMapper { //用户登陆 User findOne(User user); //用户注册 void addOne(User user); //注册时的重名检测 User checkReg(String username); }
package cn.gingost.pojo; public class User { private String username; private String userpwd; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpwd() { return userpwd; } public void setUserpwd(String userpwd) { this.userpwd = userpwd; } @Override public String toString() { return "User [username=" + username + ", userpwd=" + userpwd + "]"; } }
package cn.gingost.service; import cn.gingost.pojo.User; public interface UserService { User findOne(User user); void addOne(User user); User checkReg(String username); }
package cn.gingost.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.gingost.dao.UserMapper; import cn.gingost.pojo.User; /* * Service层,本例过于简单,其实能够不写 * 直接在Controller层中自动注入UserMapper usermapper * 使用userMapper对象直接调用方法便可 */ @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User findOne(User user) { // TODO Auto-generated method stub return userMapper.findOne(user); } @Override public void addOne(User user) { userMapper.addOne(user); // TODO Auto-generated method stub } @Override public User checkReg(String username) { // TODO Auto-generated method stub return userMapper.checkReg(username); } }
<?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="cn.gingost.dao.UserMapper"> <select id="findOne" resultType="cn.gingost.pojo.User"> select * from user where username=#{username} and userpwd=#{userpwd} </select> <update id="addOne"> insert into user values(#{username},#{userpwd}) </update> <select id="checkReg" resultType="cn.gingost.pojo.User"> select * from user where username=#{username} </select> </mapper>
一个具备注脚的文本。[^2]java
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/logandreg?useUnicode=true&characterEncoding=UTF8 user=root pwd=root maxActive=10 minIdle=5 initialSize=5
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户登陆</title> <style type="text/css"> div { width: 300px; height: 200px; margin-left: auto; margin-right: auto; } </style> </head> <body> <div> <form action="golog" method="post"> <table border="1"> <tr> <td>用户名:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密 码:</td> <td><input type="password" name="userpwd"></td> </tr> <tr> <td colspan="2" style="text-align: center;"><input type="submit" value="登陆"></td> </tr> <tr> <td colspan="2" style="text-align: center;"><a href="http://localhost:8080/LogAndReg/reg">尚未帐号?点此注册</a></td> </tr> </table> </form> </div> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户注册</title> <style type="text/css"> div { width: 300px; height: 200px; margin-left: auto; margin-right: auto; } </style> </head> <body> <div> <form action="goreg" method="post"> <table border="1"> <tr> <td>用户名:</td> <td><input type="text" name="username" ></td> </tr> <tr> <td>密 码:</td> <td><input type="password" name="userpwd" ></td> </tr> <tr> <td colspan="2" style="text-align: center;"><input type="submit" value="注册"></td> </tr> </table> </form> </div> </body> </html>
<!-- logok.jsp --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body > <h1>若是你能看见我,说明你成功了</h1> <hr> 您的用户名是:${user.username}<br> 您的密码是:${user.userpwd}<br> </body> </html> <!-- logno.jsp --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>用户名或者密码错误</h1> </body> </html> <!-- regok.jsp --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body > <h1>注册成功若是你能看见我,说明你成功了</h1><br> <a href="log">点此返回登陆界面</a> </body> </html> <!-- regno.jsp --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>该用户名已被注册</h1> </body> </html>
发布项目后,在浏览器地址栏中输入http://localhost:8080/LogAndReg/log进入登陆页面,便可开始验证工程
这里再也不展现演示结果mysql
有问题欢迎留言探讨
源码下载地址git