技术标签: Struts2
1 搭建环境:
eclipase:Luna Release (4.4.0)
sturts2:2.3.28 下载地址:http://download.csdn.net/detail/chuck_kui/9513090
2. 类:
模拟DAO : Dao.java
package com.baidu.struts2.CRUD;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
// DAO
public class Dao {
private static Map<Integer,Employee> empls = new LinkedHashMap<Integer,Employee>(); // 使用 LinkedHashMap 是为了使显示看起来的更有顺序
// 使用静态 模拟 数据库
static {
empls.put(1001,new Employee(1001, "Li", "Ming"));
empls.put(1002,new Employee(1002, "Liu", "Ming"));
empls.put(1003,new Employee(1003, "Zhang", "Ming"));
empls.put(1004,new Employee(1004, "Wang", "Ming"));
empls.put(1005,new Employee(1005, "Gong", "Ming"));
}
public List<Employee> getEmployees(){
return new ArrayList<>(empls.values());
}
public void save(Employee e){
long time = -System.currentTimeMillis();
e.setEmplId((int)time);
empls.put(e.getEmplId(), e );
}
public void delete(Integer emplId){
empls.remove(emplId);
}
public void update(Employee e ){
empls.put(e.getEmplId(),e);
}
public Employee get(Integer emplId){
return empls.get(emplId);
}
}
package com.baidu.struts2.CRUD;
// 提供实体类
public class Employee {
private Integer emplId;
private String firstName;
private String lastName;
public Employee() {
super();
}
public Employee(Integer emplId, String firstName, String lastName) {
super();
this.emplId = emplId;
this.firstName = firstName;
this.lastName = lastName;
}
public Integer getEmplId() {
return emplId;
}
public void setEmplId(Integer emplId) {
this.emplId = emplId;
}
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;
}
}
package com.baidu.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 Integer emplId;
public void setEmplId(Integer emplId) {
this.emplId = emplId;
}
public String list(){
request.put("empls", dao.getEmployees());
return "list";
}
public String save(){
dao.save(employee);
return "success";
}
public void prepareSave(){
// 为save 定制的prepare方法
employee = new Employee();
}
public String delete(){
dao.delete(emplId);
return "success";
}
/**
* edit()更新时需要回显,如何回显, 就是下面的三步
* 1. 获取传入的employeeId; employee.getEmployeeId()
* 2. 更加emplId 数据库中获取Employee 对象emp
* 3. 把栈顶的employee 对象装配好
*
* Employee emp = dao.get(employee.getEmplId());
* employee.setFirstName(emp.getFirstName());
* employee.setLastName(emp.getLastName());
*
* 现在被 ModelDriven 和 Preparable 却需要在Struts.xml 中配置 paramsPrepareParamsStack 为默认值栈
*/
public String edit(){
return "edit";
}
public void prepareEdit(){
// 为edit 定制的prepare方法
employee = dao.get(emplId);
}
public String update(){
dao.update(employee);
return "success";
}
public void prepareUpdate(){
// 为update 定制的prepare方法
employee = new Employee();
}
// 为save() 准备request
private Map<String, Object> request;
@Override
public void setRequest(Map<String, Object> arg0) {
this.request = arg0;
}
// 为edit()回显准备
private Employee employee ;
@Override
public Employee getModel() {
return employee;
}
// 为getModel() 通过model
@Override
public void prepare() throws Exception {
System.out.println("prepare....");
}
}
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">Employee All List </a>
</body>
</html>
<%@ 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>
<center>
<br><br>
<s:form action="emp-save" >
<s:textfield name="firstName" label="FirstName"></s:textfield>
<s:textfield name="lastName" label="LastName"></s:textfield>
<s:submit></s:submit>
</s:form>
<br><br>
<hr>
<table border="1" cellpadding="10" cellspacing="0">
<thead>
<tr>
<td>EmployeeID</td>
<td>FirstName</td>
<td>LastName</td>
<td>Edit</td>
<td>Delete</td>
</tr>
</thead>
<tbody>
<s:iterator value="#request.empls" var="emp">
<tr>
<td>${emplId }</td>
<td>${firstName}</td>
<td>${lastName }</td>
<td><a href="emp-edit?emplId=${emplId }">Edit</a></td>
<td><a href="emp-delete?emplId=${emplId }">Delete</a></td>
</tr>
</s:iterator>
</tbody>
</table>
</center>
</body>
</html>
<%@ 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>
<center>
<br><br>
<s:form action="emp-update" >
<s:hidden name="emplId"></s:hidden>
<s:textfield name="firstName" label="FirstName"></s:textfield>
<s:textfield name="lastName" label="LastName"></s:textfield>
<s:submit></s:submit>
</s:form>
</center>
</body>
</html>
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>
<package name="default" namespace="/" extends="struts-default">
<!-- 修改PrepareInterceptor 拦截器的 alwaysInvokePrepare 属性置为 false -->
<interceptors>
<!-- ① 为需要修改的拦截器栈起个名字 baiduStack -->
<interceptor-stack name="baiduStack">
<!-- ② baiduStack 拦截器栈 指向 paramsPrepareParamsStack-->
<interceptor-ref name="paramsPrepareParamsStack">
<!--③ 把 paramsPrepareParamsStack 拦截器栈的 prepare 拦截器 的alwaysInvokePrepare 属性值 修改为false-->
<param name="prepare.alwaysInvokePrepare">false</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!--④ 启用修改后的拦截器栈 baiduStack-->
<default-interceptor-ref name="baiduStack" />
<!-- 配置使用 paramsPrepareParamsStack 作为默认的拦截器栈
<default-interceptor-ref name="paramsPrepareParamsStack"></default-interceptor-ref>
-->
<action name="emp-*" class="com.baidu.struts2.CRUD.EmployeeAction"
method="{1}">
<result name="{1}">/emp-{1}.jsp</result>
<result name="success" type="redirectAction">emp-list</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<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>
index.jsp
emp-list.jsp
emp-edit.jsp
2.2 文字2.2.1 设置下划线示例代码:package test.create.c03;import vita.ofd.layout.OfdDocument;import vita.ofd.layout.element.Paragraph;import vita.ofd.layout.element.paragraph.Text;/** * 设置下划线示例 * */pu...
出现这样的情况的时候,U盘是可以读出来的,所以在U盘上右键单击,然后选择格式化(不要担心,格式化以后还可以恢复数据)在打开的菜单中,勾选快速格式化,然后点击开始格式化以后,我们去搜索一款数据恢复软件,我用的
这是一篇与代码并无关系的文章,从这里也并不能学习到任何方法和经验,有的就只是我个人的一点点小看法,和些许掺杂各种愤恨等情绪的牢骚。 从开始写第一篇博客到现在,差不多有小半年的时间了,写这些东西,一方面是为了记录自己学习过程中的点点滴滴,是为了积累或者是总结;另一方面,也是为了想跟大家分享一下自己学习心得,或多或少的希望可以帮到各位“新入门的码友”一点点。当我把这看成是一个自己的分享...
爬虫–HttpClient文章目录爬虫--HttpClient一、简介二、特点三、实例3.1 添加maven依赖3.2 代码实例一、简介通过HttpClient,我们可以进行网页抓取。二、特点优点:使用HttpClient高效快速。缺点:对js是不支持的,缺乏文档解析方法。通常可以作为普通的抓取方式。三、实例3.1 添加maven依赖<dependency> ...
等高布局、双飞翼布局、圣杯布局、渐进增强与优雅降级
用idea部署项目时,需要配置tomcat,有一个问题没注意启动时就报下面的错误:问题是在模板路径下并没有找到WEB-INF下的views文件,其余的都存在,开始以为是编译的问题,后来才找到根源,原来是tomcat配置的时候有个地方没有注意,点击“+”时弹出两个选项,点击artifact,有两个选项:war和war exploded二者区别:war模式:将WEB工程以包的形式上传到服务器 ;wa...
真是坑死人不偿命 百度就不用说了,我去 stackoverflow 查了半天,没有任何一个人解决了我的办法,后来我自己火眼金睛。。。鄙人用一个自己写的html 页面 利用浏览器的打印功能 去生成pdf 一直会有多余的一页空白页生成,最后发现原因如标题所述:body元素自带8px 的margin外边距!!!body元素自带8px 的margin外边距!!!body元素自带8px 的m...
在竹内亮最新拍摄的《华为的100张面孔》中,时任华为轮值CEO的郭平曾这样描述华为的危机经过2020的惊涛骇浪后,恢复稳定的华为确实有底气说出前半段话。根据2020年上半年财报数据显示,华为实现销售收入4,540亿元人民币,同比增长13.1%, 净利润率9.2 %。虽然营收同比增速出现下降(2019年上半年增长23.2%,净利润率8.7%)但总营收额和净利润率均呈现同比增长。很难相信这是在受美方制裁下,且逐渐丧失美、英、法、德等主要国外市场下取得的成就,也反映出华为的雄厚程度超过外界..
首先创建序列-- 创建序列create sequence seq_user_camera_version increment by 1 minvalue 1 no maxvalue start with 1;第二步创建表-- 建表,并用上面的序列作为主键自增序列CREATE TABLE public.user_camera_version ( id int4 NOT NULL DEFAULT nextval('seq_user_camera_version'::regclass),
STATA十八讲1-7讲1. 常用命令*--- 需求帮助 ---*helpsearch*--- 进入某路径 ---*cd*--- 设定内存 ---*set memory 20m*--- 打开和保存数据 ---*clearuessave*--- 导入数据 ---*inputeditimport*--- 重整数据 ---*appendmergexposereshapegenegenrenamedropkeepsortencodedecodeorde.
Ubuntu sim7600安装.md系统版本:ubuntu-20.04-beta-desktop-amd64下载linux内核内核版本:linux-5.6.4.tar.xz解压到 /usr/src/ 下 目录名称自动为 linux-5.6.4解压命令: cd /usr/src/ xz -d linux-5.6.4.tar.xz tar -xf linux-5.6.4.tar.xz cd linux-5.6.4系统设置启用rootsudo passwd r
2001年01月21日 14:40:00 用VC++实现异形窗口.大连铁道学院(116028)李文辉随着Microsoft凭借Windows在操作系统上取得的巨大成绩,Windows用户界面也日益成为业界标准。统一的界面给广大用户对应用软件的学习与使用带来了很大方便。但每天都面对同一副面孔,日久天长难免会产生一些厌倦,开发一些“离经叛道”,一改Windows应用程序千篇一律的“标准”界面