首页 > 学院 > 开发设计 > 正文

Struts2学习总结,做一个小demo

2019-11-06 07:37:04
字体:
来源:转载
供稿:网友

学习Struts2框架也有几天了,在这写一个demo,一来督促自己,强化记忆,二来强化练习一下,学以致用。 前言,我们在逛某宝的时候,看中我们想要的东西时,加入购物车的时候会要求我们登陆账号,当我们登陆以后才能看到我们自己的信息,收货地址,购买物品,购物车的东西等等。这里的登陆账号,也就是权限的意思,Struts可以拦截一些action请求,有效的降低服务器的负载以及有意无意的攻击。下面做一个简单的demo: 源码截图 jsp页面截图 下面是Employee.java源码

package com.whf.entityTest;import java.util.Date;/** * @author :辰 * 创建时间:2017-3-3 下午3:17:11 * */public class Employee { PRivate int id;// INT PRIMARY KEY AUTO_INCREMENT, private String empName;// VARCHAR(20), private Date workDate;// DATE -- 入职时间 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public Date getWorkDate() { return workDate; } public void setWorkDate(Date workDate) { this.workDate = workDate; }}

IEmployeeDao.java 源码

package com.whf.daoTest;import java.util.List;import com.whf.entityTest.Employee;/** * @author :辰 * 创建时间:2017-3-3 下午3:23:47 * */public interface IEmployeeDao { /** * 查询全部员工 */ List<Employee> getAll(); /** * 根据主键查询 */ Employee findById(int id); /** * 添加员工 */ void save(Employee emp); /** * 修改员工 */ void update(Employee emp);}

EmployeeDao.java源码

package com.whf.dao.implTest;import java.sql.SQLException;import java.util.List;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import com.whf.daoTest.IEmployeeDao;import com.whf.entityTest.Employee;import com.whf.utilsTest.JdbcUtils;/** * @author :杈� * 鍒涘缓鏃堕棿锛�017-3-3 涓嬪崍3:27:37 * */public class EmployeeDao implements IEmployeeDao { public Employee findById(int id) { String sql = "select * from employee where id=?"; try { return JdbcUtils.getQuerrRunner().query(sql, new BeanHandler<Employee>(Employee.class),id); } catch (SQLException e) { throw new RuntimeException(e); } } public List<Employee> getAll() { String sql = "select * from employee"; try { return JdbcUtils.getQuerrRunner().query(sql, new BeanListHandler<Employee>(Employee.class)); } catch (SQLException e) { throw new RuntimeException(e); } } public void save(Employee emp) { String sql = "insert into employee(empName,workDate) values(?,?)"; try { System.out.println("cn.itcast.dao.impl----------》》》2222222222222"); System.out.println("cn.itcast.dao.impl----------》》》成功"); JdbcUtils.getQuerrRunner().update(sql, emp.getEmpName(),emp.getWorkDate()); } catch (SQLException e) { System.out.println("cn.itcast.dao.impl----------》》》失败"); throw new RuntimeException(e); } } public void update(Employee emp) { String sql = "update employee set empName=?,workDate=? where id=?"; try { JdbcUtils.getQuerrRunner().update(sql, emp.getEmpName(),emp.getWorkDate(),emp.getId()); } catch (SQLException e) { throw new RuntimeException(e); } }}

IEmployeeService.java源码

package com.whf.dao.serviceTest;import java.util.List;import com.whf.entityTest.Employee;/** * @author :辰 * 创建时间:2017-3-3 下午3:23:47 * 业务逻辑接口 * * */public interface IEmployeeService { /** * 查询全部员工 */ List<Employee> getAll(); /** * 根据主键查询 */ Employee findById(int id); /** * 添加员工 */ void save(Employee emp); /** * 修改员工 */ void update(Employee emp);}

EmployeeService.java源码

package com.whf.service.implTest;import java.util.List;import com.whf.dao.implTest.EmployeeDao;import com.whf.dao.serviceTest.IEmployeeService;import com.whf.daoTest.IEmployeeDao;import com.whf.entityTest.Employee;/** * @author :辰 * 创建时间:2017-3-3 下午4:36:26 * */public class EmployeeService implements IEmployeeService{ private IEmployeeDao employeeDao = new EmployeeDao(); public Employee findById(int id) { try { return employeeDao.findById(id); } catch (Exception e) { throw new RuntimeException(e); } } public List<Employee> getAll() { try { return employeeDao.getAll(); } catch (Exception e) { throw new RuntimeException(e); } } public void save(Employee emp) { try { employeeDao.save(emp); System.out.println("cn.itcast.service.impl--------成功-------33333333333333"); } catch (Exception e) { System.out.println("cn.itcast.service.impl--------失败"); throw new RuntimeException(e); } } public void update(Employee emp) { try { employeeDao.update(emp); } catch (Exception e) { throw new RuntimeException(e); } }}

JdbcUtils.java源码

package com.whf.utilsTest;import javax.sql.DataSource;import org.apache.commons.dbutils.QueryRunner;import com.mchange.v2.c3p0.ComboPooledDataSource;/** * @author :辰 * 创建时间:2017-3-3 下午3:19:31 * */public class JdbcUtils { //初始化连接池 private static DataSource dataSource; static{ dataSource = new ComboPooledDataSource(); } public static DataSource getDataSource(){ return dataSource; } /** * 创建DbUtils常用工具对象 */ public static QueryRunner getQuerrRunner(){ return new QueryRunner(dataSource); }}

c3p0-config.xml文件配置

<c3p0-config> <default-config> <property name="driverClass">com.MySQL.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///hib_demo</property> <property name="user">root</property> <property name="passWord">123456</property> <property name="initialPoolSize">5</property> <property name="maxPoolSize">10</property> </default-config> <named-config name="OracleConfig"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///hib_demo</property> <property name="user">root</property> <property name="password">123456</property> <property name="initialPoolSize">5</property> <property name="maxPoolSize">10</property> </named-config></c3p0-config>

EmployeeAction.java源码

package com.whf.ActionTest;import java.util.List;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.util.ValueStack;import com.whf.dao.serviceTest.IEmployeeService;import com.whf.daoTest.IEmployeeDao;import com.whf.entityTest.Employee;import com.whf.service.implTest.EmployeeService;/** * @author :辰 * 创建时间:2017-3-3 下午4:44:16 * 员工管理Action */public class EmployeeAction extends ActionSupport implements ModelDriven<Employee>{ /****封装数据****/ private Employee employee = new Employee(); public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } // 重写模型驱动方法 public Employee getModel() { return employee; } /****调用的Service****/ private IEmployeeService employeeService = new EmployeeService(); /** * 1. 添加员工 */ public String save() { try { System.out.println("调用service保存-------->11111"); // 调用service保存 employeeService.save(employee); // 添加成功,去到列表页面 System.out.println("调用service保存-------->成功"); return list(); //return "addsuccess"; } catch (Exception e) { e.printStackTrace(); System.out.println("调用service保存-------->失败"); return ERROR; } } /** * 2. 列表显示 */ public String list() { try { // 查询全部 List<Employee> listEmp = employeeService.getAll(); // 保存到request域 ActionContext.getContext().getContextMap().put("listEmp", listEmp); System.out.println("显示查询List全部-----------》成功"); return "list"; } catch (Exception e) { e.printStackTrace(); System.out.println("显示查询List全部-----------》失败"); return ERROR; } } /** * 3. 进入修改页面 */ public String viewUpdate() { try { // 3.1 获取当前修改的记录的主键值 int id = employee.getId(); // 3.2 service查询 Employee emp = employeeService.findById(id); // 3.3 数据回显 // a. 先得到值栈 ValueStack vs = ActionContext.getContext().getValueStack(); vs.pop(); //移除栈顶元素 vs.push(emp); // emp对象放入栈顶 return "update"; } catch (Exception e) { e.printStackTrace(); return ERROR; } } /** * 4. 修改员工 */ public String update() { try { // 调用service修改 employeeService.update(employee); return list(); } catch (Exception e) { e.printStackTrace(); return ERROR; } }}

add.java源码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="/struts-tags" prefix="s" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>Add</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <s:form action="/emp_save" method="post"> <!-- 防止表单重复提交,第一步:生成id(客户端、服务器) --> <s:token></s:token> <table> <tr> <td>员工名:</td> <td><s:textfield name="empName" /></td> </tr> <tr> <td>日期:</td> <td><s:textfield name="workDate" /></td> </tr> <tr> <td colspan="2"> <s:submit value="保存员工"></s:submit> </td> </tr> </table> </s:form> </body></html>

list.jsp源码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="/struts-tags" prefix="s" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>list</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <table border="1" align="center"> <tr> <th>序号</th> <th>编号</th> <th>员工名称</th> <th>日志日期</th> <th>操作</th> </tr> <!-- 1. 先判断;2. 再迭代 --> <s:if test="#request.listEmp != null"> <s:iterator var="emp" value="#request.listEmp" status="st"> <tr> <td><s:property value="#st.count"/></td> <td><s:property value="#emp.id"/></td> <td><s:property value="#emp.empName"/></td> <td><s:property value="#emp.workDate"/></td> <td> <s:a href="emp_viewUpdate?id=%{#emp.id}">修改</s:a> </td> </tr> </s:iterator> </s:if> <s:else> <tr> <td colspan="5">对不起,没有你要显示的数据</td> </tr> </s:else> </table> </body></html>

update.jsp源码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="/struts-tags" prefix="s" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>Add</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <s:form action="/emp_update" method="post"> <!-- 隐藏域,保存主键 --> <s:hidden name="id"></s:hidden> <table> <tr> <td>员工名:</td> <td><s:textfield name="empName" /></td> </tr> <tr> <td>日期:</td> <!-- <td><s:date name="workDate" format="yyyy-MM-dd"/> <s:hidden name="workDate"></s:hidden> </td> --> <td> <s:textfield name="workDate" /> </td> </tr> <tr> <td colspan="2"> <s:submit value="修改员工"></s:submit> </td> </tr> </table> </s:form> </body></html>

以上就是一个Struts2简单demo


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表