1.目录
2.需要导的包
3 web.xml
springmvc_spring_mybatis_001
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
springDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springDispatcherServlet
/
字符集过滤器
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
字符集编码
encoding
UTF-8
encodingFilter
/*
4 springmvc.xml
5 db.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=gbk&useSSL=true&verifyServerCertificate=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
6 applicationContext.xml
7 com.mw.ssm.domain包下建User类
package com.mw.ssm.domain;
public class User {
private int id;
private String name;
private String pwd;
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 getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
8 com.mw.ssm.dao目录下建UserDao接口
package com.mw.ssm.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.mw.ssm.domain.User;
@Repository("userDao")
public interface UserDao {
public List getList();
public boolean addUser(User user);
public int deleteUser(int id);
}
9 com.mw.ssm.xml包下建User.xml数据访问层文件
insert into user(id,name,pwd) values(#{id},#{name},#{pwd})
delete from user where id = #{id}
10 com.mw.ssm.service目录下建UserService接口
package com.mw.ssm.service;
import java.util.List;
import com.mw.ssm.domain.User;
public interface UserService {
public List getList();
public boolean addUser(User user);
public String deleteUser(int id);
}
11 com.mw.ssm.service.impl目录下建UserService接口的实现类
package com.mw.ssm.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mw.ssm.dao.UserDao;
import com.mw.ssm.domain.User;
import com.mw.ssm.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List getList() {
return userDao.getList();
}
@Override
public boolean addUser(User user) {
return userDao.addUser(user);
}
@Override
public String deleteUser(int id) {
if (userDao.deleteUser(id) == 1) {
return "删除成功";
}
return "删除失败";
}
}
12 com.mw.ssm.controller包下建控制层UserController类
package com.mw.ssm.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mw.ssm.domain.User;
import com.mw.ssm.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
public void setUserService(UserService userService){
this.userService = userService;
}
@RequestMapping("/list")
public String getList(ModelMap map){
map.addAttribute("list", userService.getList());
return "/list.jsp";
}
@RequestMapping("addUser")
public String addUser(HttpServletRequest request, User user){
Boolean boolean1 = userService.addUser(user);
if (boolean1) {
return "/list.jsp";
}else {
return "/error.jsp";
}
}
@SuppressWarnings("finally") //用于抑制编译器的警告信息
@RequestMapping("/deleteUser")
public String deleteUser(HttpServletRequest request, int id){
try {
String str = userService.deleteUser(id);
request.setAttribute("InfoMessage", str);
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("InfoMessage", "删除信息失败!具体异常信息:"+e.getMessage());
}finally {
return "result";
}
}
}
13 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
---${loginUser}---
新增用户
14 add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
添加用户
15 list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'list.jsp' starting page
所有用户所有信息
所有用户所有信息
id
姓名
密码
操作
${user.id }
${user.name }
${user.pwd }
更新 删除
进入用户管理页
16 result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
结果页
${InfoMessage}
返回首页