Struts的增删改查

Posted xhpcc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts的增删改查相关的知识,希望对你有一定的参考价值。

准备工作:导入通用分页的tag类,以及d.tld,还有完成xml的配置,在导入我们昨天导入的xml

xml;

<web-app 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"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>encodingFiter</filter-name>
<filter-class>com.caoguangli.crud.util.EncodingFiter</filter-class>
</filter>

<filter-mapping>
<filter-name>encodingFiter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>

先建立一个实体类(clazz)添加属性(图片给个默认值)再写clazzdao,在clazzdao补充增删改查即可,再用ClazzAction调用dao层给前台传数据,再配置昨天导入的struts_sy.xml后写完JSP页面接收数据就完成了

 

 

代码:

clazzaction:

 

 

package .xhp.clazzaction;

import java.sql.SQLException;
import java.util.List;

import com.caoguangli.crud.dao.ClazzDao;
import com.caoguangli.crud.entity.Clazz;
import com.caoguangli.crud.util.BaseAction;
import com.caoguangli.crud.util.PageBean;
import com.opensymphony.xwork2.ModelDriven;

public class ClazzAction extends BaseAction implements ModelDriven<Clazz> 

private Clazz clazz = new Clazz();
private ClazzDao clazzDao = new ClazzDao();

/**
* 查询方法
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public String list() throws InstantiationException, IllegalAccessException, SQLException 
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
List<Clazz> list = this.clazzDao.list(clazz, pageBean);
request.setAttribute("clazzList", list);
request.setAttribute("pageBean", pageBean);
return "list";


/**
* 跳转编辑页面(新增修改页面)
* @return
*/

public String preSave() 
if(clazz.getCid() != 0) 
try 
this.result = this.clazzDao.list(clazz, null).get(0);
 catch (InstantiationException | IllegalAccessException | SQLException e) 
// TODO Auto-generated catch block
e.printStackTrace();


return "preSave";




public String add() 
try 
this.clazzDao.add(clazz);
 catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) 
// TODO Auto-generated catch block
e.printStackTrace();

return "toList";



public String edit() 
try 
this.clazzDao.edit(clazz);
 catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) 
// TODO Auto-generated catch block
e.printStackTrace();

return "toList";


public String del() 
try 
this.clazzDao.del(clazz);
 catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) 
// TODO Auto-generated catch block
e.printStackTrace();

return "toList";




@Override
public Clazz getModel() 
// TODO Auto-generated method stub
return clazz;

 

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>班级编辑界面</title>
</head>
<body>
<form action="$pageContext.request.contextPath$result.cname == null ? ‘/sy/clazz_add.action‘ : ‘/sy/clazz_edit.action‘ " method="post">
    <%-- <input type="hidden" name="methodName" value="$book.bname == null ? ‘add‘ : ‘edit‘ "> --%>
    cid:<input type="text" name="cid" value="$result.cid "><br>
    cname:<input type="text" name="cname" value="$result.cname "><br>
    cteacher:<input type="text" name="cteacher" value="$result.cteacher "><br>
    <input type="submit">
</form>
</body>
</html>

完成

以上是关于Struts的增删改查的主要内容,如果未能解决你的问题,请参考以下文章

struts的增删改查

MySQL-增删改查简易操作

MySQL-增删改查简易操作

Struts的增删改查

6.实现struts2+hibernate实现学生列表的增删改查

struts spring hibernate 三大框架实现基本的增删改查技术