java框架Struts学习--struts开发流程

来源:互联网 发布:java写入文本文件 编辑:程序博客网 时间:2024/05/28 19:23

1. web项目,引入struts - jar


2. web.xml中,引入struts的核心功能

<?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>

3. 开发action

import java.util.List;import com.bxh.entity.Employee;import com.bxh.service.IEmployeeService;import com.bxh.service.impl.EmployeeService;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.util.ValueStack;//员工管理Actionpublic class EmployeeAction extends ActionSupport implements ModelDriven<Employee>{/*封装数据*/private Employee employee=new Employee();public void setEmp(Employee employee) {this.employee = employee;}public Employee getEmp() {return employee;}/**调用service***/private IEmployeeService employeeService=new EmployeeService();public String save(){try {employeeService.save(employee);return list();//return "addsuccess";} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return ERROR;}}//显示列表页面public String list(){try {List<Employee> listEmp=employeeService.getAll();ActionContext.getContext().getContextMap().put("listEmp", listEmp);return "list";} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return ERROR;}}//实现驱动模型方法public Employee getModel() {// TODO Auto-generated method stubreturn employee;}//进入修改页面public String viewUpdate(){try {int id=employee.getId();Employee emp=employeeService.findById(id);//数据回显技术ValueStack vs=ActionContext.getContext().getValueStack();vs.pop();vs.push(emp);return "update";} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return ERROR;}}//修改成功后进入显示页面public String update(){try {employeeService.update(employee);return list();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return ERROR;}}}


4. 配置action


src/struts.xml


<?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"><!-- START SNIPPET: xworkSample --><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="com.bxh.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><result name="addsuccess" type="redirectAction">emp_list</result></action></package></struts><!-- END SNIPPET: xworkSample -->