需要配置的有 : Spring监听器, SpringMVC 核心前端控制器, 字符集过滤器
SSM
index.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
springmvcDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
springmvcDispatcherServlet
*.action
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
driverClass=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/ssm
user=root
pwd=root
# Global logging configuration
#\u5728\u5f00\u53d1\u73af\u5883\u4e0b\u65e5\u5fd7\u7ea7\u522b\u8981\u8bbe\u7f6e\u6210DEBUG\uff0c\u751f\u4ea7\u73af\u5883\u8bbe\u7f6e\u6210info\u6216error
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
package top.vkiss.ssm.domain;
public class User {
private Integer uid;
private String uname;
private String phone;
private String address;
// 这里省略了getter / setter 方法
}
package top.vkiss.ssm.mapper;
import java.util.List;
import top.vkiss.ssm.domain.User;
public interface UserMapper {
public void addUser(User user) throws Exception;
public void delUser(Integer id) throws Exception;
public void editUser(User user) throws Exception;
public User getUser(Integer id) throws Exception;
public List queryAll() throws Exception;
}
uid, uname, phone, address
delete from user
where uid = #{uid,jdbcType=INTEGER}
insert into user
uid,
uname,
phone,
address,
#{uid,jdbcType=INTEGER},
#{uname,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
#{address,jdbcType=VARCHAR},
update user
uname = #{uname,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
address = #{address,jdbcType=VARCHAR},
where uid = #{uid,jdbcType=INTEGER}
package top.vkiss.ssm.service;
import java.util.List;
import top.vkiss.ssm.domain.User;
public interface UserService {
public void addUser(User user) throws Exception;
public void delUser(Integer id) throws Exception;
public void editUser(User user) throws Exception;
public User getUser(Integer id) throws Exception;
public List queryAll() throws Exception;
}
package top.vkiss.ssm.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.vkiss.ssm.domain.User;
import top.vkiss.ssm.mapper.UserMapper;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper ud;
@Override
public void addUser(User user) throws Exception {
// TODO Auto-generated method stub
ud.addUser(user);
}
@Override
public void delUser(Integer id) throws Exception {
// TODO Auto-generated method stub
ud.delUser(id);
}
@Override
public void editUser(User user) throws Exception {
// TODO Auto-generated method stub
ud.editUser(user);
}
@Override
public User getUser(Integer id) throws Exception {
// TODO Auto-generated method stub
return ud.getUser(id);
}
@Override
public List queryAll() throws Exception {
// TODO Auto-generated method stub
return ud.queryAll();
}
}
package top.vkiss.ssm.controller;
import java.util.List;
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 top.vkiss.ssm.domain.User;
import top.vkiss.ssm.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService us;
@RequestMapping("/queryAll")
public String queryAll(Model model) throws Exception{
model.addAttribute("uList", us.queryAll());
return "shouye";
}
@RequestMapping("/addUser")
public String addUser(User user) throws Exception {
System.out.println("addUser:"+user);
us.addUser(user);
return "redirect:queryAll.action";
}
@RequestMapping("/delUser")
public String delUser(Integer id) throws Exception {
System.out.println("del_id:"+id);
us.delUser(id);
return "redirect:queryAll.action";
}
@RequestMapping("/delS")
public String delS(int[] ids) throws Exception {
System.out.println("del_id:"+ids);
for (int i : ids) {
us.delUser(i);
}
return "redirect:queryAll.action";
}
@RequestMapping("/editUser")
public String editUser(User user) throws Exception {
System.out.println(user.getUname());
us.editUser(user);
return "redirect:queryAll.action";
}
@RequestMapping("/getUser")
public String getUser(Model model,Integer id) throws Exception {
System.out.println(id);
System.out.println("...."+us.getUser(id));
model.addAttribute("u", us.getUser(id));
return "edit";
}
@RequestMapping("/showAdd")
public String showAdd() throws Exception {
return "addUs";
}
}
package top.vkiss.ssm.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 本项目中没有用到,仅作学习使用
*/
@Component("task")
public class myTask {
@Scheduled(cron="* * * * * *")
public void zhh(){
System.out.println("999999999999999");
}
}
index.jsp 项目启动用于重定向跳转
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
response.sendRedirect("queryAll.action");
%>
My JSP 'index.jsp' starting page
This is my JSP page.
shouye,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 'index.jsp' starting page
This Is My First SSM Application.
addUs.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 'index.jsp' starting page
edit.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 'index.jsp' starting page