7.Struts2_CRUD操作(1)查询和删除

先导入struts2必要的包放在lib下,并配置web.xml

1.结构目录

2.代码

(1)index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<a href="emp-list.action">直接跳转页面</a>
</body>
</html>

(2)emp-list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<table cellpadding="10" cellspacing="0" border="1">
		<thead>
			<tr>
				<td>ID</td>
				<td>FirstName</td>
				<td>LastName</td>
				<td>Email</td>
				<td>Edit</td>
				<td>Delete</td>
			</tr>
		</thead>
		
		<body>
		
			<s:iterator value="#request.emps">
			<tr>
				<td>${employeeId}</td>
				<td>${firstName}</td>
				<td>${lastName}</td>
				<td>${email}</td>
				<td><a href="">Edit</a></td>
				<td><a href="emp-delete?employeeId=${employeeId}">Delete</a></td>			
			</tr>
			</s:iterator>
		
		</body>
	</table>

</body>
</html>

(3)Employee.java

package struts.app;

public class Employee {
	private Integer employeeId;
	private String firstName;
	private String lastName;
	private String 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;
	}
	public Employee(Integer employeeId, String firstName, String lastName, String email) {
		super();
		this.employeeId = employeeId;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
	}
	
	public Employee() {     super();}
	
	
}

(4).Dao.java

package struts.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Dao {
	
	private static Map<Integer,Employee> emps=new HashMap<Integer,Employee>();
	
	static {
			emps.put(1001, new Employee(1001,"aa","AA","aa@qq.com"));
			emps.put(1002, new Employee(1002,"bb","BB","bb@qq.com"));
			emps.put(1005, new Employee(1005,"ff","FF","ff@qq.com"));
			
			emps.put(1004, new Employee(1004,"dd","DD","dd@qq.com"));
			emps.put(1003, new Employee(1003,"cc","CC","cc@qq.com"));
			
	}
	
	public List<Employee> getEmployee(){
		return new ArrayList<>(emps.values());
	}
	
	public void delete(Integer empId) {
		emps.remove(empId);
	}
	 
	public void save(Employee emp) {
		long time=System.currentTimeMillis();
		emp.setEmployeeId((int)time);
		
		emps.put(emp.getEmployeeId(),emp);
	}

	public Employee get(Integer empId) {
		return emps.get(empId);
		
	}
	
	public void update(Employee emp) {
		emps.put(emp.getEmployeeId(),emp);
	}
}

(5).EmployeeAction.java

package struts.app;

import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;

import com.opensymphony.xwork2.ModelDriven;

public class EmployeeAction implements RequestAware{
	
	private Dao dao=new Dao();
	
	//需要在当前的EmployeeAction中定义employeeId的属性。
	//以接收请求参数
	private Integer employeeId;
	
	public void setEmployeeId(Integer employeeId) {
		this.employeeId=employeeId;
	}
	

	public String delete() {
		dao.delete(employeeId);
		//返回结果的类型应为:redirectAction.
		//也可以是chain:实际上chain是没有必要的,因为不需要在下一个Action中保存当前的Action
		//还有,若使用chain,则到达目标页面后,地址栏显示的依然是删除的那个链接,刷新时会有重复提交。
		return "success";
	}
	
	
	public String list() {
	request.put("emps",dao.getEmployee());
	return "list";
	}
	
	private Map<String,Object> request;
	@Override
	public void setRequest(Map<String, Object> arg0) {
		this.request=arg0;
		
	}

	
	
}

struts.xml

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

<struts>
	<package name="struts" namespace="" extends="struts-default">
		<action name="emp-*" class="struts.app.EmployeeAction" method="{1}">
			<result name="{1}">/emp-{1}.jsp</result>
			<result name="success" type="redirectAction">/emp-list</result>
		</action>
		

	</package>
</struts> 

页面显示:


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值