接:
接下来配置类
UserController
package com.chatRotbot.controller; import com.chatRotbot.model.User; import com.chatRotbot.service.IUserService; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; 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 org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpSession; @Controller public class UserController { private static Logger logger = Logger.getLogger(UserController.class); @Autowired private IUserService userService; @RequestMapping("/gotoLogin") public String gotoLogin(){ return "login"; } @RequestMapping("/login") public String login(@RequestParam(value="loginName" ,required=true) String loginName, String password, Model model, HttpSession session){ logger.info("登陆用户名:"+loginName); //用redirect和forward标签返回 //注意:用这两个标签必须写全路径,不适应视图解析的逻辑视图 if(StringUtils.isNotEmpty(loginName)&& StringUtils.isNotEmpty(password)){ User user = userService.loginUser(loginName, password); if(user!=null){ logger.info("登陆成功"); session.setAttribute("user", user); return "redirect:/main" ; }else{ model.addAttribute("loginFlag","登陆失败,请输入正确的用户名和密码"); return "forward:/WEB-INF/pages/login.jsp"; } }else{ model.addAttribute("loginFlag","登陆失败,请输入正确的用户名和密码"); return "forward:/WEB-INF/pages/login.jsp"; } } @RequestMapping("/main") public String main(){ return "main/main"; } }
IUserService
package com.chatRotbot.service; import com.chatRotbot.model.User; public interface IUserService { /** * 登陆验证 * @param loginName * @param password * @return */ public User loginUser(String loginName,String password); }
UserServiceImpl
package com.chatRotbot.service.impl; import com.chatRotbot.dao.mapper.UserMapper; import com.chatRotbot.model.User; import com.chatRotbot.service.IUserService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class UserServiceImpl implements IUserService { @Resource private UserMapper userMapper; /** * 登录 * @param loginName * @param password * @return */ public User loginUser(String loginName, String password) { User user = new User(); user.setUserName(loginName); List<User> userList = userMapper.getUserList(user); if(userList.isEmpty()){ return null; }else{//进行密码比对 String ecnryptPassword = userList.get(0).getPassword(); if(ecnryptPassword.equals(password)) { return userList.get(0); } else { return null; } } } }
UserMapper dao文件中的文件编码
import com.chatRotbot.model.User;
import java.util.List;
public interface UserMapper {
/**
* 根据条件查询用户列表
* @param user
* @return
*/
List <User> getUserList(User user);
}
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"> <!-- 设置为IUserDao接口方法提供sql语句配置 --> <mapper namespace="com.chatRotbot.dao.mapper.UserMapper"> <sql id = "all_user_columns"> id,email,password,username,role,status,regTime,regIp </sql> <!--将通用的sql查询条件抽取出来,成为sql片段 ,给不同方法应用 --> <sql id="query_user_sql_where"> <if test="userName!=null"> AND username LIKE '%${userName}%' </if> <if test="password!=null"> AND PASSWORD= #{password} </if> </sql> <select id="getUserList" parameterType="user" resultType="user"> SELECT <include refid="all_user_columns"></include> FROM user <where> <include refid="query_user_sql_where"></include> </where> </select> </mapper>
User
package com.chatRotbot.model; import java.util.Date; public class User { public long getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } private int userId; private String email; private String password; private String userName; private String role; private int status; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public Date getRegTime() { return regTime; } public void setRegTime(Date regTime) { this.regTime = regTime; } public String getRegIp() { return regIp; } public void setRegIp(String regIp) { this.regIp = regIp; } private Date regTime; private String regIp; }
login.jsp 登录界面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录</title> <style type="text/css"> html,body{height:100%;} /*canvas开始*/ #canvas {position: absolute;z-index: -1;display: block;} /*canvas结束*/ #login { width: 400px; height: 320px; background: #fff; position: absolute; /*绝对定位*/ margin:auto; top:0px; left:0px; right:0px; bottom:0px; box-shadow: 0px 0px 10px #666; border-radius: 4px; /*圆角*/ } .well{background-color: #062A36;} h2{font-family:"微软雅黑";font-size:24px;color:#B94846;margin-bottom:28px;} #loginForm{color:#fff} </style> </head> <body style = "border-radius: 10px;background-size:100% 100% "> <div class="row" id="login" > <div class="well col-md-12 center"> <div class="title" style="padding: 5px;text-align:center;"> <h2>登录系统</h2> </div> <div id="messageBoxLogin" class="alert alert-error ${empty loginFlag ? 'hide' :''} "> <label id="loginFlag" class="error">${loginFlag}</label> </div> <form id="loginForm" action="login" method="post"> <label class="input-label" for="loginName">登录名</label> <input type="text" id="loginName" name="loginName" value=""> <label class="input-label" for="password">密码</label> <input type="password" id="password" name="password" > <input class="btn btn-primary" type="submit" value="登 录" /> </form> </div> </div> </body> </html>
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head>项目搭建</head> <body> <h1>恭喜您,ssm框架搭建成功</h1> </body> </html>
新建完成后配置项目运行环境,点击Run-Edit Configurations...
点击加号新建运行环境,选择Tomcat Server-Local
选中新建好的服务器,右边选择Deployment,点击加号-Atifact...
选择第二项
war和
war exploded
这里看一下他们两个的区别:
war模式:将WEB工程以包的形式上传到服务器 ;
war exploded模式:将WEB工程以当前文件夹的位置关系上传到服务器;
(1)war模式这种可以称之为是发布模式,看名字也知道,这是先打成war包,再发布;
(2)war exploded模式是直接把文件夹、jsp页面 、classes等等移到Tomcat 部署文件夹里面,进行加载部署。因此这种方式支持热部署,一般在开发的时候也是用这种方式。
(3)在平时开发的时候,使用热部署的话,应该对Tomcat进行相应的设置,这样的话修改的jsp界面什么的东西才可以及时的显示出来。
然后在右边Application context配置你的项目名
终于配置完了,运行Tomcat我们看一下效果吧。
我们启动以后会自动弹出登录界面,这里登录界面有点难看,勿喷啊。
当我们输入密码错误时出现以下界面
当我们输入正确的密码时,界面跳转到 主界面
现在项目算是告一段落了。
打印日志:
2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:861) - DispatcherServlet with name 'springmvc' processing POST request for [/ChatRobot/login] 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:319) - Looking up handler method for path /login 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:326) - Returning handler method [public java.lang.String com.chatRotbot.controller.UserController.login(java.lang.String,java.lang.String,org.springframework.ui.Model,javax.servlet.http.HttpSession)] 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:250) - Returning cached instance of singleton bean 'userController' 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.cors.DefaultCorsProcessor.processRequest(DefaultCorsProcessor.java:72) - Skip CORS processing, request is a same-origin one 2018/09/13/17:43:04 INFO [http-bio-8081-exec-2] com.chatRotbot.controller.UserController.login(UserController.java:30) - 登陆用户名:chenyaping 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:54) - Creating a new SqlSession 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:54) - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@167558b0] was not registered for synchronization because synchronization is not active 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:110) - Fetching JDBC Connection from DataSource 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:54) - JDBC Connection [com.mysql.jdbc.Connection@5407451a] will not be managed by Spring 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:54) - ==> Preparing: SELECT id,email,password,username,role,status,regTime,regIp FROM user WHERE username LIKE '%chenyaping%' 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:54) - ==> Parameters: 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:54) - <== Total: 2 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.debug(JakartaCommonsLoggingImpl.java:54) - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@167558b0] 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection(DataSourceUtils.java:327) - Returning JDBC Connection to DataSource 2018/09/13/17:43:04 INFO [http-bio-8081-exec-2] com.chatRotbot.controller.UserController.login(UserController.java:38) - 登陆成功 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1620) - Invoking afterPropertiesSet() on bean with name 'redirect:/main' 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:250) - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1241) - Rendering view [org.springframework.web.servlet.view.RedirectView: name 'redirect:/main'; URL [/main]] in DispatcherServlet with name 'springmvc' 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1000) - Successfully completed request 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:861) - DispatcherServlet with name 'springmvc' processing GET request for [/ChatRobot/main] 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:319) - Looking up handler method for path /main 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:326) - Returning handler method [public java.lang.String com.chatRotbot.controller.UserController.main()] 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:250) - Returning cached instance of singleton bean 'userController' 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:947) - Last-Modified value for [/ChatRobot/main] is: -1 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1620) - Invoking afterPropertiesSet() on bean with name 'main/main' 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:250) - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1241) - Rendering view [org.springframework.web.servlet.view.JstlView: name 'main/main'; URL [/WEB-INF/pages/main/main.jsp]] in DispatcherServlet with name 'springmvc' 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:166) - Forwarding to resource [/WEB-INF/pages/main/main.jsp] in InternalResourceView 'main/main' 2018/09/13/17:43:04 DEBUG [http-bio-8081-exec-2] org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1000) - Successfully completed request
总结:
我这里只是简单的搭建了一个 ssm框架,实现页面的登录功能,这才是项目开始冰山一角并且还有很多地方的不足,这里在搭建这个登录功能时也遇到了很多的问题,比如刚开始搭建环境时,Maven版本下的最新版本导致
IntelliJ IDEA一直无法读取,最后下载了低一点版本,还遇到mapper文件一直无法读取,最终参考https://blog.csdn.net/u010504064/article/details/47337307,希望这个简单的搭建能给大家一些帮助和启发。