版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/84997347
模拟数据库的意思就是用集合里面的元素充当数据库里面的数据;
第一步、搭建Struts2的环境
https://mp.csdn.net/postedit/84889898
第二步、实体类+模拟数据库+在页面显示集合里面的数据
POJO
package cn.com.app;
public class Employ {
private Integer employId;
private String employname;
private String lastname;
private String email;
public Integer getEmployId() {
return employId;
}
public void setEmployId(Integer employId) {
this.employId = employId;
}
public String getEmployname() {
return employname;
}
public void setEmployname(String employname) {
this.employname = employname;
}
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 Employ(Integer employId, String employname, String lastname, String email) {
super();
this.employId = employId;
this.employname = employname;
this.lastname = lastname;
this.email = email;
}
public Employ() {
// TODO Auto-generated constructor stub
}
}
集合充当数据库
这里有几个注意的地方
1、为什么用hashmap,不用list集合?
当删除或者修改的时候涉及到id,根据Id修改,如果用到的集合是list的形式的话,取id就会变得很麻烦,但是如果是hashmap,把key和id设置一样的话,就是十分容易了;
2、为什么获取hashmap里面的数据返回的是List集合不是map集合的形式
在页面上我们现在学习的都是list形式,如果在页面上循环遍历的是hashmap的话,就会很复杂,没有必要;
package cn.com.app;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Maps;
public class HashEmploy {
/*author:命运的信徒
* date:2018/12/12
* arm:struts2的crud
*/
private static Map<Integer,Employ> maps=new HashMap<Integer,Employ>();
//以下代码不能放在getmaps()方法里面,不然的话就会导致一直删除不掉
static{
maps.put(1,new Employ(1, "田", "周", "[email protected]"));
maps.put(2,new Employ(2, "王", "有", "[email protected]"));
maps.put(3,new Employ(3, "周", "说", "[email protected]"));
maps.put(4,new Employ(4, "李", "都", "[email protected]"));
maps.put(5,new Employ(5, "刘", "年", "[email protected]"));
}
//查询功能
public List<Employ> getmaps(){
return new ArrayList<Employ>(maps.values());
/*上述操作如同
*
* List<Employ> list=new ArrayList<Employ>();
list.addAll(maps.values());
*/
}
//删除功能
public void delete(Integer id){
maps.remove(id);
}
//增加功能
/*把放到集合里面的数据的时间设置为Id,就可以保证不重复了,然后传参Employ e,设置e.setEmployId(id);就可以了
*
*
*/
public void add(Employ e){
Integer id=(int) new Date().getTime();
e.setEmployId(id);
maps.put(id, e);
}
//修改功能
/*
* 由于修改的只会是内容,不可能是id,所以只需要替换以下就可以了,hashmap里面是不能有重复id的数据
* 传递的参数的Employ e
*/
public void update(Employ e){
Integer id=e.getEmployId();
maps.put(id, e);
}
//根据id查询
public Employ select(Integer id){
Employ es=maps.get(id);
return es;
}
/*public static void main(String[] args) {
Map<Integer,Employ> map=new HashMap<Integer,Employ>();
map.put(1,new Employ(1, "田", "周", "[email protected]"));
map.put(2,new Employ(2, "王", "有", "[email protected]"));
map.put(3,new Employ(3, "周", "说", "[email protected]"));
map.put(4,new Employ(4, "李", "都", "[email protected]"));
map.put(5,new Employ(5, "刘", "年", "[email protected]"));
map.remove(1);
List<Employ> lists=new ArrayList<Employ>();
lists.addAll(map.values());
for (Employ e : lists) {
System.out.println(e.getEmployId());
}
}
*/
}
action
package cn.com.app;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import com.opensymphony.xwork2.ModelDriven;
public class EmployAction implements RequestAware, ModelDriven<Employ> {
private Map<String, Object> request;
private HashEmploy hash = new HashEmploy();
private Employ employ;
public Map<String, Object> getRequest() {
return request;
}
@Override
public void setRequest(Map<String, Object> arg0) {
// TODO Auto-generated method stub
request = arg0;
}
// query查询数据
public String getall() {
List<Employ> list = hash.getmaps();
request.put("list", list);
return "getall";
}
// 删除
public String delete() {
// 获取id,然后删除
hash.delete(employ.getEmployId());
return "success";
}
// 编辑数据
/*
* 无论是更新数据还是增加数据的难点都在于方法传递的是Employ对象,而从页面传递过来的是employid,employname属性,
* 最笨的方法就是把这些方法写在Employ的实体类中,但是,我们在实际开发的过程中,是不可能放在一起的,所以我们得想一个方法
* ModelDrivern拦截器,会把返回的对象放置栈顶
*/
public String update() {
hash.update(employ);
return "success";
}
public String updates() {
// 根据id来查询出值
Employ es=hash.select(employ.getEmployId());
System.out.println(es.getEmployname());
request.put("es", es);
return "updates";
}
public String addinfo() {
hash.add(employ);
return "success";
}
@Override
public Employ getModel() {
// TODO Auto-generated method stub
employ = new Employ();
return employ;
}
}
点击连接的页面
<a href="list-getall.action">list all info</a>
配置文件
<?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>
<!-- 这个是注明.action,和.do 结尾都可以应答 -->
<constant name="struts.action.extension" value="do,action,kk"></constant>
<package name="tjn" extends="struts-default">
<action name="gg" class="cn.com.action.Person" method="execute">
<!-- result="名称" 当名称是error的时候,代表着对应的是<result name="error"></result>这个信息;exception="异常全类名"-->
<!-- <exception-mapping result="error" exception="java.lang.ArithmeticException">
</exception-mapping>
<result name="error">/context.jsp</result> -->
<result name="success">/sort.jsp</result>
</action>
<action name="map" class="cn.com.action.RequestTest" >
<result>/one.jsp</result>
</action>
<action name="list-*" class="cn.com.app.EmployAction" method="{1}">
<result name="{1}">/list-{1}.jsp</result>
<result name="success" type="redirectAction">/list-getall.kk</result>
</action>
</package>
</struts>
显示页面(查询与添加和删除)
<body>
<h1>添加信息表单</h1>
<s:form action="list-addinfo.kk">
<s:textfield name="employname" label="employname"></s:textfield>
<s:textfield name="lastname" label="lastname"></s:textfield>
<s:textfield name="email" label="email"></s:textfield>
<s:submit></s:submit>
</s:form>
<hr>
<hr>
<table>
<thead>
<tr>
<td>employId</td>
<td>employname</td>
<td>lastname</td>
<td>email</td>
<td><a href="list-add">增加</a>
</td>
</tr>
</thead>
<tbody>
<s:iterator value="#request.list" var="a">
<tr>
<td>${a.employId}</td>
<td>${a.employname}</td>
<td>${a.lastname}</td>
<td>${a.email}</td>
<td><a href="list-delete.kk?employId=${a.employId}">删除</a>
</td>
<td><a href="list-updates.kk?employId=${a.employId}">更新</a>
</td>
</tr>
</s:iterator>
</tbody>
</table>
</body>
修改页面
<body>
<h1>更新页面</h1>
<s:form action="list-update.kk">
<s:textfield name="employId" label="employId" value="%{#request.es.employId}"></s:textfield>
<s:textfield name="employname" label="employname" value="%{#request.es.employname}"></s:textfield>
<s:textfield name="lastname" label="lastname" value="%{#request.es.lastname}"></s:textfield>
<s:textfield name="email" label="email" value="%{#request.es.email}"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
最终效果截图
知识点科普:
1.Action 实现ModelDriven接口后的运行流程
1)先会执行ModelDrivenInterceptor的intercept方法
2)Action已经实现了ModelDriven接口,调用getModel()方法,将对象放在栈顶
这里涉及到把前端的jsp页面的参数放到栈顶对象的属性里面
1)执行ParametersInterceptor的intercept方法:把请求参数的值给栈顶对象对应的属性。若栈顶对象的属性对应不上,就找值栈总下一个对象的属性
2)注意:getModedl()方法不能提供以下实现。的确返回一个employ对象到值栈的栈顶,但employ的属性都是null的!