boolean login(String name, String pwd); void insertUser(User user); List<User> selectUser(); void updateUser(User user); boolean deleteUser(String name); public User findByName(String name);
接口实现类:UserServiceImpl
public class UserServiceImpl implements UserService { UserDao dao = new UserDaoImpl(); public boolean login(String name, String pwd) { return dao.login(name, pwd); } public void insertUser(User user) { dao.insertUser(user); } public List<User> selectUser() { return dao.selectUser(); } public void updateUser(User user) { dao.updateUser(user); } public boolean deleteUser(String name) { return dao.deleteUser(name); } public User findByName(String name) { return dao.findByName(name); } }
控制层:UserAction
public class UserAction extends ActionSupport{ private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } UserService userService = new UserServiceImpl(); public String selectUser(){ HttpServletRequest request = ServletActionContext.getRequest(); List<User> users = new ArrayList<User>(); users = userService.selectUser(); request.setAttribute("USER", users); return "select"; } /** * 登陆 * @return */ public String login(){ if(user.getName() != null && user.getPwd() != null) { boolean flag = userService.login(user.getName(), user.getPwd()); if(flag) { return SUCCESS; } } return ERROR; } /** * 修改 * @return */ public String update(){ userService.updateUser(user); return "update"; } /** * 编辑 * @return */ public String edit(){ HttpServletRequest request = ServletActionContext.getRequest(); String name = request.getParameter("uName"); if(name != null) { User u = userService.findByName(name); request.setAttribute("USER", u); } return "edit"; } /** * 删除 * @return */ public String delete(){ HttpServletRequest request = ServletActionContext.getRequest(); String name = request.getParameter("uName"); boolean flag = userService.deleteUser(name); System.out.println(flag); return SUCCESS; } /** * 添加 * @return */ public String insert(){ userService.insertUser(user); return "insert"; } }
struts.xml配置(本人用的的struts2):
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package extends="struts-default"> <action method="selectUser"> <result>/list.jsp</result> </action> <action method="login"> <result type="redirectAction">/list.action</result> <result>/login.jsp</result> </action> <action method="insert"> <result type="redirectAction">/list.action</result> </action> <action method="delete"> <result type="redirect">/list.action</result> </action> <action method="update"> <result type="redirectAction">/list.action</result> </action> <action method="edit"> <result>/update.jsp</result> </action> </package> </struts>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee "> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
四个页面:login.jsp list.jsp insert.jsp,update.jsp
login.jsp