Strust2 搭建一个简单CRUD(增、查、更、删)的操作网页

1、搭建Struts2基本环境

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

1.2、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
	http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.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>


	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>


</web-app>

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<Integer, Employee> emps = new HashMap<Integer, Employee>();

	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<Employee> 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<Employee>, 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<String, Object> requset;

	private Integer employeeId;

	public void setEmployeeId(Integer employeeId) {
		this.employeeId = employeeId;
	}

	/**
	 * 获取Request对象的方式,这里选择实现RequestAware接口。
	 */
	@Override
	public void setRequest(Map<String, Object> request) {
		this.requset = request;

		// ServletActionContext.getRequest()紧耦合的获取方式,获取的是HttpServletRequest对象
		// 获取的是Map<String, Object>对象
		// 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"%>
<!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>

	<s:debug></s:debug>
	<s:form action="emp-save">
		<s:textfield name="firstName" label="FirstName"></s:textfield>
		<s:textfield name="lastName" label="LastName"></s:textfield>
		<s:textfield name="email" label="Email"></s:textfield>
		<s:submit></s:submit>
	</s:form>

	<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>
		<tbody>
			<s:iterator value="#request.emps">
				<tr>
					<td>${employeeId }</td>
					<td>${firstName }</td>
					<td>${lastName }</td>
					<td>${email }</td>
					<td><a href="emp-edit?employeeId=${employeeId }">Edit</td>
					<td><a href="emp-delete?employeeId=${employeeId }">Delete</td>
				</tr>
			</s:iterator>
		</tbody>
	</table>
</body>
</html>
5、写一个Emp-edit.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>
	<s:form action="emp-update">
		<s:hidden name="employeeId"></s:hidden>
		<s:textfield name="firstName" label="FirstName"></s:textfield>
		<s:textfield name="lastName" label="LastName"></s:textfield>
		<s:textfield name="email" label="Email"></s:textfield>
		<s:submit></s:submit>
	</s:form>
</body>
</html>

6、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">


<struts>
	<!-- Overwrite Convention -->

	<package name="default" extends="struts-default" namespace="/">

		<interceptors>
			<interceptor-stack name="splend">
				<interceptor-ref name="paramsPrepareParamsStack">
					<param name="prepare.alwaysInvokePrepare">false</param>
				</interceptor-ref>
			</interceptor-stack>
		</interceptors>

		<default-interceptor-ref name="splend"></default-interceptor-ref>
		<action name="emp-*" class="com.splend.struts2.crud.EmployeeAction"
			method="{1}">
			<result name="success" type="redirectAction">emp-list</result>
			<result name="{1}">/emp-{1}.jsp</result>
		</action>
	</package>
</struts>
7、写一个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=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="emp-list">List All Employees</a>
</body>
</html>
7、项目结构


8、运行index.jsp:




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值