您的位置:首页 > 编程语言 > Java开发

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

2017-08-28 16:41 375 查看
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;

//员工管理Action
public 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 block
e.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 block
e.printStackTrace();
return ERROR;
}

}
//实现驱动模型方法
public Employee getModel() {
// TODO Auto-generated method stub
return 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 block
e.printStackTrace();
return ERROR;
}

}
//修改成功后进入显示页面
public String update(){
try {
employeeService.update(employee);
return list();
} catch (Exception e) {
// TODO Auto-generated catch block
e.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 -->
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息