继续对上一版本进行改版,变成SpringMVC框架
简易的CRM系统案例之易的CRM系统案例之JSP+MySQL+SSH框架版本
src/spring.xml
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<import resource="config/bean-base.xml"/>
<import resource="config/bean-dao.xml"/>
<import resource="config/bean-entity.xml"/> <!-- Action控制器 -->
<context:component-scan base-package="com.loaderman.crm"/> <!-- 基于注解的映射器(可选) -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <!-- 基于注解的适配器(可选) -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- 视图解析器(可选) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/> </beans>
package com.loaderman.crm.action; import com.loaderman.crm.entity.User;
import com.loaderman.crm.service.impl.UserServiceimp;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
public class GetUserListAction {
@Autowired
private UserServiceimp userService; // 处理请求
@RequestMapping(value = "/getUserList")
public String getUserList(Model model, HttpServletRequest request) throws Exception {
String name = request.getParameter("name");
if ( name != null && (! name.isEmpty())) {
List<User> list = userService.getUserByName( name);
model.addAttribute("list",list);
} else {
List<User> list = userService.getAllUser();
model.addAttribute("list",list); } return "/pages/userList.jsp";
}
}
package com.loaderman.crm.action; import com.loaderman.crm.entity.User;
import com.loaderman.crm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; @Controller
public class AddUserAction { @Autowired
private UserService userService;
@RequestMapping(value = "/addUser")
public String addUser(User user, HttpServletRequest request1) throws Exception {
System.out.println(user.toString());
String opr = request1.getParameter("opr");
int n = 0;
if (opr.equals("addUser")) {
n = userService.addUser(user);
} else if (opr.equals("modifyUser")) {
int id = Integer.parseInt(request1.getParameter("id"));
user.setId(id);
n = userService.modifyUser(user);
}
if (n > 0) {
return "redirect:getUserList.action";
} else {
return "redirect:addUser.action";
}
}
}
点击源码下载