第一步:按照前文搭建好SSM框架后,创建数据库test、表格Member。如图:
第二步:在common层创建工具类 ,字符串工具类:StringUtils.java,内容如下:
package com.bigname.common.utils;
/**
* 字符串工具类
* @version 创建时间:2019年6月4日 下午12:17:53
*/
public class StringUtils {
public static boolean isNullOrZero(String str) {
boolean b = false;
if (str.length() == 0 || str.equals("") || str == null){
b = true;
}
return b;
}
}
第三步:在core层创建实体类 Member.java ,注意要与数据库字段对应。内容如下:
package com.bigname.demo03.core;
public class Member {
private int id;
private String name;
private String password;
public Member(){}
public Member(int id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Member [id=" + id + ", name=" + name + ", password=" + password
+ "]";
}
}
第四步:在dao层创建数据访问类 。内容如下:
a.创建接口:MemberDao.java
package com.bigname.demo03.dao;
import org.apache.ibatis.annotations.Param;
import com.bigname.demo03.core.Member;
public interface MemberDao {
Member selectMemberByName(@Param("name")String name)throws Exception;
}
b.在mapper中创建MemberDaoMapper.xml
第四步:在function创建业务类 。内容如下:
a.创建Member业务接口。
package com.bigname.demo03.function;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bigname.demo03.core.Member;
@Service("IMemberFunction")
@Transactional(readOnly = true)
public interface IMemberFunction {
Member login(String name) throws Exception;
}
b.创建Member业务接口实现类。
package com.bigname.demo03.function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bigname.common.utils.StringUtils;
import com.bigname.demo03.core.Member;
import com.bigname.demo03.dao.MemberDao;
@Service
public class MemberFunctionImpl implements IMemberFunction{
@Autowired
private MemberDao mDao;
public Member login(String name) throws Exception {
System.out.println(name);
if(StringUtils.isNullOrZero(name)){
System.out.println("用戶名不能為空");
return null;
}
Member member = mDao.selectMemberByName(name);
return member;
}
}
第四步:在controller创建LoginController,定义接口 。内容如下:
package com.bigname.demo03.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bigname.demo03.core.Member;
import com.bigname.demo03.function.IMemberFunction;
@Controller
@RequestMapping(value = "LoginController")
public class LoginController {
@Autowired
IMemberFunction iMemberFunc;
@RequestMapping(value = "/hello")
public String hello(){
System.out.println("接收到请求 ,Hello");
return "hi";
}
/**
* 访问登录界面
* @return 返回登录界面名称
*/
@RequestMapping(value = "/index")
public String index(){
System.out.println("=============index");
return "index";
}
/**
* 登录验证方法
* @param name
* @param password
* @return 返回结果界面名称
*/
@RequestMapping(value = "/login")
public String login(String name, String password){
try {
Member member = iMemberFunc.login(name);
if(member == null){
System.out.println("登陆失败");
return "index";
}else {
if(member.getPassword().equals(password)){
System.out.println("登陆成功");
return "login";
}else{
System.out.println("登陆失败");
return "index";
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println("登录失败");
return "index";
}
}
}
第五步:在WEB-INF文件夹新建jsp文件夹,存放jsp文件。在jsp文件夹里新建index.jsp和login.jsp。内容如下:
(1)index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登录页面
(2)login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
系统主页
欢迎登录!
第六步:注意添加配置
(1)在spring-mvc.xml文件中添加配置:
(2)在web.xml文件中添加配置:
spring-mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
true
spring-mvc
*.do
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
/index.html
/index.jsp
第七步:运行并访问项目:http://localhost:8080/zeyu/LoginController/index.do。如图:
输入账号密码,可验证登录。
上一篇《基于maven搭建SSM框架(三) ——添加框架所需依赖》