Struts2 综合案例

1.总结: 1 ModelDriven 来封装前台数据, 通过struts2的参数拦截器,封装到action中的成员变量中,在写方法中,千万不要写参数否则会报找不到save,update等方法的错误,我找了近近一个小时才找到!

action:

public class EmployeeAction extends ActionSupport implements ModelDriven<Employee>{

	private IEmployeeService employeeService = new EmployeeService();
	
	/*****封装数据**************/
	private Employee emp = new Employee();
	public void setEmp(Employee emp) {
		this.emp = emp;
	}
	public Employee getEmp() {
		return emp;
	}
	public Employee getModel() {
		return emp;
	}
		
	public String save(){//不能加参数 save(Employee emp)
		try {
			//1.调用service
			employeeService.save(emp);
			//2.跳转页面
			return list();
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String list(){
		try {
			//1.去db中拿所有的员工数据
			List<Employee> lists = employeeService.getAll();
			//2.存入域对象中传到前台去 request域
			ActionContext.getContext().getContextMap().put("lists", lists);
			
			return "list";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
//记住千万不能这样写, public String update(Employee emp){...},否则会报找不到这个方法的错误!参数是前台传过的来的,直接封装到了成员变量中了,
	public String update(){
		try {
			employeeService.update(emp);
			return list();
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	public String edit(){
		try{
			//1.获取当前修改的记录的主键值
			int id =emp.getId();
			//2.调用service查询
			Employee emp = employeeService.findById(id);
			//3. 数据回显 通过值栈
			ValueStack vs = ActionContext.getContext().getValueStack();
			vs.pop();
			vs.push(emp);
			return "update";
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
}

  2.serviceimpl  省略接口

public class EmployeeService implements IEmployeeService {

	IEmployeeDao employeeDao = new EmployeeDao();
	public List<Employee> getAll() {
		
		return employeeDao.getAll();
	}

	public Employee findById(int id) {
		
		return employeeDao.findById(id);
	}

	public void save(Employee emp) {
		employeeDao.save(emp);

	}

	public void update(Employee emp) {
		employeeDao.update(emp);

	}

	public IEmployeeDao getEmployeeDao() {
		return employeeDao;
	}

	public void setEmployeeDao(IEmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}
	
}

  3.daoimpl 省略接口

public class EmployeeDao implements IEmployeeDao {

	public List<Employee> getAll() {
		String sql = "select * from employees";
		try {
			return JDBCUtils.getQueryRunner().query(sql, new BeanListHandler<Employee>(Employee.class));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public Employee findById(int id) {
		String sql = "select * from employees where id = ?";
		try {
			return JDBCUtils.getQueryRunner().query(sql, new BeanHandler<Employee>(Employee.class),id);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void save(Employee emp) {
		String sql = "insert into employees (empName,workDate) values (?,?)";
		try {
			JDBCUtils.getQueryRunner().update(sql, emp.getEmpName(),emp.getWorkDate());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}

	public void update(Employee emp) {
		String sql = "update employees set empName=?,workDate=? where id=? ";
		try {
			JDBCUtils.getQueryRunner().update(sql, emp.getEmpName(),emp.getWorkDate(),emp.getId());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

}

  4.数据库连接:

public class JDBCUtils {
	
	//1.初始化连接池
	private static DataSource dataSource;
	static{
		dataSource = new ComboPooledDataSource();
	}
	public static DataSource getDataSource() {
		return dataSource;
	}
	//2.创建一个DBUtils常用工具类对象
	public static QueryRunner getQueryRunner(){
		return  new QueryRunner(dataSource);
	} 
}

  

5. entity 省略setter/getter 方法

public class Employee {
	private int id;
	private String empName;
	private Date workDate;
}

  

二:1.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 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 	<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>

 2.struts.xml  奇怪为什么package的name一去就报错!!!为什么不是写包名?  s:token 防止request的重复提交一共三步,用session一步搞定!

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<constant name="struts.ui.theme" value="simple"></constant>
	<package name="emp" extends="struts-default">
		<global-results>
			<result name="error">/error/error.jsp</result>
		</global-results>
		
		<action name="emp_*" class="cn.itcast.action.EmployeeAction" method="{1}">
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<interceptor-ref name="token">
				<param name="includeMethods">save</param>
			</interceptor-ref>
			<result name="invalid.token" type="redirectAction">emp_list</result>
			<result name="list">/WEB-INF/list.jsp</result>
			<result name="update">/WEB-INF/update.jsp</result>
		</action>
	</package>
</struts>

  3.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">root</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:///day17</property> 
     <property name="user">root</property> 
     <property name="password">root</property> 
     <property name="initialPoolSize">5</property> 
     <property name="maxPoolSize">10</property> 
   </named-config>

</c3p0-config>

  三:

1.add.jsp

    <s:form action="/emp_save" method="post">
    	<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>

  2.list.jsp

<body>
    <table border="1" align="center">
    	<tr>
    		<th>序号</th>
    		<th>编号</th>
    		<th>员工名</th>
    		<th>入职时间</th>
    		<th>操作</th>
    	</tr>
    	<!-- 先判断后迭代 -->
    	<s:if test="#request.lists !=null">
    		<s:iterator var="emp" value="#request.lists" 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_edit?id=%{#emp.id}">修改</s:a>
    				</td>
    			</tr>
    		</s:iterator>
    	</s:if>
    	<s:else>
    		<tr><td colspan="5">sorry,没有你要的数据</td></tr>
    	</s:else>
    </table>
  </body>

  3.update.jsp

    <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:textfield name="workDate"/></td>
    		</tr>
    		<tr>   			
    			<td colspan="2"><s:submit value="修改员工"/></td>
    		</tr>
    	</table>
    </s:form>

  四:效果图

1.add.jsp

2. list.jsp

3.edit.jsp

4.update.jsp-->list.jsp

 

 

posted @ 2016-09-01 00:55  黑土白云  阅读(315)  评论(0编辑  收藏  举报