struts2综合案例

黑土白云 黑土白云     2022-08-02     197

关键词:

1.总结: 1 ModelDriven 来封装前台数据, 通过struts2的参数拦截器,封装到action中的成员变量中,在写方法中,千万不要写参数否则会报找不到save,update等方法的错误,我找了近近一个小时才找到!

action:

public class EmployeeAction extends ActionSupport implements ModelDriven<Employee>{

	private IEmployeeService employeeService = new EmployeeService();
	
	/*****封装数据**************/
	private Employee emp = new Employee();
	public void setEmp(Employee emp) {
		this.emp = emp;
	}
	public Employee getEmp() {
		return emp;
	}
	public Employee getModel() {
		return emp;
	}
		
	public String save(){//不能加参数 save(Employee emp)
		try {
			//1.调用service
			employeeService.save(emp);
			//2.跳转页面
			return list();
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String list(){
		try {
			//1.去db中拿所有的员工数据
			List<Employee> lists = employeeService.getAll();
			//2.存入域对象中传到前台去 request域
			ActionContext.getContext().getContextMap().put("lists", lists);
			
			return "list";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
//记住千万不能这样写, public String update(Employee emp){...},否则会报找不到这个方法的错误!参数是前台传过的来的,直接封装到了成员变量中了,
	public String update(){
		try {
			employeeService.update(emp);
			return list();
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	public String edit(){
		try{
			//1.获取当前修改的记录的主键值
			int id =emp.getId();
			//2.调用service查询
			Employee emp = employeeService.findById(id);
			//3. 数据回显 通过值栈
			ValueStack vs = ActionContext.getContext().getValueStack();
			vs.pop();
			vs.push(emp);
			return "update";
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
}

  2.serviceimpl  省略接口

public class EmployeeService implements IEmployeeService {

	IEmployeeDao employeeDao = new EmployeeDao();
	public List<Employee> getAll() {
		
		return employeeDao.getAll();
	}

	public Employee findById(int id) {
		
		return employeeDao.findById(id);
	}

	public void save(Employee emp) {
		employeeDao.save(emp);

	}

	public void update(Employee emp) {
		employeeDao.update(emp);

	}

	public IEmployeeDao getEmployeeDao() {
		return employeeDao;
	}

	public void setEmployeeDao(IEmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}
	
}

  3.daoimpl 省略接口

public class EmployeeDao implements IEmployeeDao {

	public List<Employee> getAll() {
		String sql = "select * from employees";
		try {
			return JDBCUtils.getQueryRunner().query(sql, new BeanListHandler<Employee>(Employee.class));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public Employee findById(int id) {
		String sql = "select * from employees where id = ?";
		try {
			return JDBCUtils.getQueryRunner().query(sql, new BeanHandler<Employee>(Employee.class),id);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void save(Employee emp) {
		String sql = "insert into employees (empName,workDate) values (?,?)";
		try {
			JDBCUtils.getQueryRunner().update(sql, emp.getEmpName(),emp.getWorkDate());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}

	public void update(Employee emp) {
		String sql = "update employees set empName=?,workDate=? where id=? ";
		try {
			JDBCUtils.getQueryRunner().update(sql, emp.getEmpName(),emp.getWorkDate(),emp.getId());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

}

  4.数据库连接:

public class JDBCUtils {
	
	//1.初始化连接池
	private static DataSource dataSource;
	static{
		dataSource = new ComboPooledDataSource();
	}
	public static DataSource getDataSource() {
		return dataSource;
	}
	//2.创建一个DBUtils常用工具类对象
	public static QueryRunner getQueryRunner(){
		return  new QueryRunner(dataSource);
	} 
}

  

5. entity 省略setter/getter 方法

public class Employee {
	private int id;
	private String empName;
	private Date workDate;
}

  

二:1.web.xml

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

</web-app>

 2.struts.xml  奇怪为什么package的name一去就报错!!!为什么不是写包名?  s:token 防止request的重复提交一共三步,用session一步搞定!

<?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>
	<constant name="struts.ui.theme" value="simple"></constant>
	<package name="emp" extends="struts-default">
		<global-results>
			<result name="error">/error/error.jsp</result>
		</global-results>
		
		<action name="emp_*" class="cn.itcast.action.EmployeeAction" method="{1}">
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<interceptor-ref name="token">
				<param name="includeMethods">save</param>
			</interceptor-ref>
			<result name="invalid.token" type="redirectAction">emp_list</result>
			<result name="list">/WEB-INF/list.jsp</result>
			<result name="update">/WEB-INF/update.jsp</result>
		</action>
	</package>
</struts>

  3.c3p0-config.xml

<c3p0-config>
  <default-config>
     <property name="driverClass">com.mysql.jdbc.Driver</property> 
     <property name="jdbcUrl">jdbc:mysql:///hib_demo</property> 
     <property name="user">root</property> 
     <property name="password">root</property> 
     <property name="initialPoolSize">5</property> 
     <property name="maxPoolSize">10</property> 

  </default-config>


  <named-config name="oracleConfig">
    <property name="driverClass">com.mysql.jdbc.Driver</property> 
     <property name="jdbcUrl">jdbc:mysql:///day17</property> 
     <property name="user">root</property> 
     <property name="password">root</property> 
     <property name="initialPoolSize">5</property> 
     <property name="maxPoolSize">10</property> 
   </named-config>

</c3p0-config>

  三:

1.add.jsp

    <s:form action="/emp_save" method="post">
    	<s:token></s:token>
    	<table>
    		<tr>
    			<td>员工 </td>
    			<td><s:textfield name="empName"/></td>
    		</tr>
    		<tr>
    			<td>入职时间 </td>
    			<td><s:textfield name="workDate"/></td>
    		</tr>
    		<tr>   			
    			<td colspan="2"><s:submit value="保存员工"></s:submit></td>
    		</tr>
    	</table>
    </s:form>

  2.list.jsp

<body>
    <table border="1" align="center">
    	<tr>
    		<th>序号</th>
    		<th>编号</th>
    		<th>员工名</th>
    		<th>入职时间</th>
    		<th>操作</th>
    	</tr>
    	<!-- 先判断后迭代 -->
    	<s:if test="#request.lists !=null">
    		<s:iterator var="emp" value="#request.lists" status="st">
    			<tr>
    				<td><s:property value="#st.count"/></td>
    				<td><s:property value="#emp.id"/></td>
    				<td><s:property value="#emp.empName"/></td>
    				<td><s:property value="#emp.workDate"/></td>
    				<td>
    					<s:a href="emp_edit?id=%{#emp.id}">修改</s:a>
    				</td>
    			</tr>
    		</s:iterator>
    	</s:if>
    	<s:else>
    		<tr><td colspan="5">sorry,没有你要的数据</td></tr>
    	</s:else>
    </table>
  </body>

  3.update.jsp

    <s:form action="/emp_update" method="post">
    	<s:hidden name="id"></s:hidden>
    	<table>
    		<tr>
    			<td>员工 </td>
    			<td><s:textfield name="empName"/></td>
    		</tr>
    		<tr>
    			<td>入职时间 </td>
    			<td><s:textfield name="workDate"/></td>
    		</tr>
    		<tr>   			
    			<td colspan="2"><s:submit value="修改员工"/></td>
    		</tr>
    	</table>
    </s:form>

  四:效果图

1.add.jsp

2. list.jsp

3.edit.jsp

4.update.jsp-->list.jsp

 

 

javaee框架技术之14ssm综合案例产品管理crud(代码片段)

...握】产品管理二、SSM整合Spring+SpringMVC+Mybatis–>SSMSpring+Struts2+Hibernate-->SSH2.1简单整合在学习springmvc时我们发现,spring 查看详情

案例11:高层综合楼防火案例分析

案例11:高层综合楼防火案例分析(一) 建筑分类和耐火等级:总平面布局  防火分区:消防水泵设置规定 锅炉房,变压器  查看详情

Struts2 传递变量案例

】Struts2传递变量案例【英文标题】:Struts2passingvariablescase【发布时间】:2015-03-1118:25:25【问题描述】:我正在使用Datatables服务器端ajax分页,需要将一些变量传递给服务器。我的服务器正在运行Struts2操作来处理这个数据表请求... 查看详情

struts2入门案例

Struts2是常用的web层框架,jar包下载路径开发包目录结构介绍在web工程中引入struts2的开发包如何在web。xml中配置struts2的核心过滤器<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter 查看详情

css3_综合案例

综合案例设置元素的width,还可以利用left和right 为了防止图片太小,background-size:100%100%;                  查看详情

acl的综合应用案例

...例说明了标准ACL、扩展ACL、命名ACL的配置案例,下面介绍综合应用ACL的案例。 ACL的原理与基本配置的链接:http://yangshufan.blog.51cto.com/13004230/1958558ACL的综合应用 公司内部网络已经建成,网络拓扑图如下所示:650)this.width=65... 查看详情

黑马前端pinkhtml综合案例:圣诞节的那些事小说排行榜案例注册页面(代码片段)

文章目录综合案例1:圣诞节的那些事目标代码综合案例2:小说排行榜案例目标代码综合案例3:注册页面目标代码综合案例1:圣诞节的那些事视频p30-31目标代码<!DOCTYPEhtml><htmllang="en"><head><... 查看详情

struts2框架运行流程及案例

Struts2框架Struts2由Struts1和WebWork两个经典的MVC框架发展起来,是一个非常优秀的MVC框架。Struts2中的execute()方法不再与servletAPI耦合,因而更容易测试。Struts2支持更多的视图技术,提供了基于AOP思想的拦截机制,以及更强大更容易... 查看详情

struts2第一个入门案例

 一.如何获取Struts2,以及Struts2资源包的目录结构的了解  Struts的官方地址为http://struts.apache.org在他的主页当中,我们可以通过左侧的ApacheStruts菜单下的Release链接,可以查看Struts各个阶段的词资源,也可以通过ArchiveSite... 查看详情

学习react-简单小案例--综合案例

<html><head><title></title><metacharset="UTF-8"/><scriptsrc="js/react.min.js"type="text/javascript"charset="utf-8"></script><scriptsrc="js/react-dom.min.js 查看详情

struts2第一个入门小案例

 1.加载类库2配置web.xml文件3.开发视图层4.开发控制层Action5.配置struts.xml6.部署运行  查看详情

set集合综合案例

案例01:生成0-10之间5个不相等的数方法01:使用list集合实现importrandomlist01=[]foriinrange(100):   num01=random.randint(0,10)   ifnum01notinlist01:       list 查看详情

proteus仿真51单片机电子锁综合设计案例

【Proteus仿真】51单片机电子锁综合设计案例 查看详情

struts2入门案例

1:导入对应的核心jar包  2:配置Web<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=" 查看详情

set集合综合案例

案例01:生成0-10之间5个不相等的数方法1:使用list集合实现importrandomlist01=[]foriinrange(100):   num01=random.randint(0,10)   ifnum01notinlist01:       list0 查看详情

struts2第一个入门案例

最近刚刚学习了Struts2的一些基本内容下面我来利用Struts2实现一个小例子想要写Struts2的代码Jia包必不可少如下:8个Jia包必不可少然后就是配置了,大家都知道框架就是配置红色标记的用意是只要是修改的jsp页面的东西都不用重... 查看详情

angularjs综合笔记案例

AngularJS表达式 (1.{{}} 2.ng-bind(单项绑定))AngularJS表达式写在双大括号内:{{expression}}AngularJS使用表达式把数据绑定到HTML,这与 ng-bind 指令有异曲同工之妙。AngularJS将在表达式书写的位置"输出"数据。AngularJS表达式&... 查看详情

django—综合案例(代码片段)

案例效果如下:打开/booktest/显示书籍列表点击新增,增加一条数据点击删除,删除一条数据点击查看,跳转英雄信息界面1.定义模型类打开booktest/models.py文件,定义模型类如下fromdjango.dbimportmodels#Createyourmodelshere.#定义书籍模型类... 查看详情