博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Strust2 搭建一个简单CRUD(增、查、更、删)的操作网页
阅读量:4128 次
发布时间:2019-05-25

本文共 5011 字,大约阅读时间需要 16 分钟。

1、搭建Struts2基本环境

1.1、导入Strust2 Jar包我使用Strust2.3

1.2、配置web.xml文件

Struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
Struts2
/*
index.jsp

2、编写一个Dao类进行数据访问(为了方面直接不进行连接数据库,从Map进行读取)。

package com.splend.struts2.crud;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * @author Splendid * */public class Dao {		private static Map
emps = new HashMap
(); static { emps.put(1001, new Employee(1001, "AA", "aa", "aa@splend.com")); emps.put(1002, new Employee(1002, "BB", "bb", "bb@splend.com")); emps.put(1003, new Employee(1003, "CC", "cc", "cc@splend.com")); emps.put(1004, new Employee(1004, "DD", "dd", "dd@splend.com")); emps.put(1005, new Employee(1005, "EE", "ee", "ee@splend.com")); emps.put(1006, new Employee(1006, "FF", "ff", "ff@splend.com")); } public void save(Employee emp) { long time = System.currentTimeMillis(); emp.setEmployeeId(((int) (time))%100+1007); emps.put(emp.getEmployeeId(), emp); } public List
getEmployees() { return new ArrayList<>(emps.values()); } public void delete(Integer employeeId) { emps.remove(employeeId); } public Employee get(Integer empId) { return emps.get(empId); } public void update(Employee emp) { emps.put(emp.getEmployeeId(), emp); }}
3、编写一个Employee的JavaBean

Employee.java:

package com.splend.struts2.crud;public class Employee {	private Integer employeeId;	private String firstName;	private String lastName;	private String email;	public Employee() {	}	public Employee(Integer employeeId, String firstName, String lastName, String email) {		super();		this.employeeId = employeeId;		this.firstName = firstName;		this.lastName = lastName;		this.email = email;	}	public Integer getEmployeeId() {		return employeeId;	}	public void setEmployeeId(Integer employeeId) {		this.employeeId = employeeId;	}	public String getFirstName() {		return firstName;	}	public void setFirstName(String firstName) {		this.firstName = firstName;	}	public String getLastName() {		return lastName;	}	public void setLastName(String lastName) {		this.lastName = lastName;	}	public String getEmail() {		return email;	}	public void setEmail(String email) {		this.email = email;	}}

4、编写EmployeeAction

EmployeeAction.java

package com.splend.struts2.crud;import java.util.Map;import org.apache.struts2.interceptor.RequestAware;import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.Preparable;public class EmployeeAction implements RequestAware, ModelDriven
, Preparable { private Dao dao = new Dao(); private Employee employee; public String edit() { return "edit"; } public void prepareEdit() { employee = dao.get(employeeId); } public String update() { dao.update(employee); return "success"; } public void prepareUpdate() { employee = new Employee(); } public String save() { dao.save(employee); return "success"; } public void prepareSave() { employee = new Employee(); } public String delete() { dao.delete(employeeId); return "success"; } public String list() { requset.put("emps", dao.getEmployees()); return "list"; } private Map
requset; private Integer employeeId; public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } /** * 获取Request对象的方式,这里选择实现RequestAware接口。 */ @Override public void setRequest(Map
request) { this.requset = request; // ServletActionContext.getRequest()紧耦合的获取方式,获取的是HttpServletRequest对象 // 获取的是Map
对象 // ActionContext.getContext().get("request"); } /** * 使用ModelDriven把employee对象放到值栈栈顶。 */ @Override public Employee getModel() { return employee; } @Override public void prepare() throws Exception { // TODO Auto-generated method stub }}
5、写一个Emp-list.jsp进行显示用户信息

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%>
Insert title here
ID FirstName LastName Email Edit Delete
${employeeId } ${firstName } ${lastName } ${email } Edit Delete
5、写一个Emp-edit.jsp进行编辑和提交

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%>
Insert title here

6、src下创建一个struts.xml

false
emp-list
/emp-{1}.jsp
7、写一个index.jsp首页

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title here List All Employees
7、项目结构

8、运行index.jsp:

你可能感兴趣的文章
X-code7 beta error: warning: Is a directory
查看>>
Error: An App ID with identifier "*****" is not avaliable. Please enter a different string.
查看>>
X-code beta 开发iWatch项目,运行没有错误,但是某些操作一点就崩,而且找不错误的原因场景一
查看>>
Xcode 报错: Extra argument in call
查看>>
iTunes Connect 上传APP报错: Communication error. please use diagnostic mode to check connectivity.
查看>>
#import <Cocoa/Cocoa.h> 报错 Lexical or Preprocessor Issue 'Cocoa/Cocoa.h' file not found
查看>>
`MQTTClient (~> 0.2.6)` required by `Podfile`
查看>>
X-Code 报错 ld: library not found for -lAFNetworking
查看>>
Bitcode
查看>>
If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
查看>>
3.5 YOLO9000: Better,Faster,Stronger(YOLO9000:更好,更快,更强)
查看>>
iOS菜鸟学习--如何避免两个按钮同时响应
查看>>
How to access the keys in dictionary in object-c
查看>>
iOS菜鸟学习—— NSSortDescriptor的使用
查看>>
hdu 3787 hdoj 3787
查看>>
hdu 3790 hdoj 3790
查看>>
hdu 3789 hdoj 3789
查看>>
hdu 3788 hdoj 3788
查看>>
zju 1003 zoj 1003
查看>>
zju 1004 zoj 1004
查看>>