java框架Struts学习--struts开发流程
2017-08-28 16:41
375 查看
1. web项目,引入struts - jar包
![](http://img.blog.csdn.net/20170828164232641?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzIyNjEzOTk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
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 -->
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 -->
相关文章推荐
- 【转载】struts 学习总结----开发流程
- Struts2学习笔记--Struts例子及开发流程
- struts学习之说trust2开发流程
- 【转载】struts 学习总结----开发流程
- struts 学习总结----开发流程
- Struts 2 框架学习 第1part 基本开发流程
- [学习]对小规模团队的开发流程设计
- [树莓派内核学习]Qt的开发流程
- 【学习】[连载]软件开发流程知多少 之一 需求定义篇
- WinCE板级支持包开发学习之WinCE的初始化驱动流程
- java新手web开发学习笔记--基础知识和(struts,hibeinate,spring)ssh框架流程
- 最近在学习struts+spring+iBatis开发
- 学习笔记 4_Cocos Creator_脚本开发工作流程
- struts学习笔记(二):搭建struts开发环境
- Struts学习心得之Struts流程篇(3) -示例 续 转载
- 【安卓SDK学习之anyChatSDK】 1_1 实现用户登录和房间进出(根据官方HelloAnyChat的demo整理的简要的开发流程)
- 敏捷开发系列学习总结(11)——Scrum敏捷开发流程的三个角色、四个会议和三个物件
- 嵌入式学习-嵌入式的组成特点,开发流程等
- Bada学习- C++以及Flash应用开发流程之创建应用工程
- 深度学习项目开发流程