Struts2值crud(简易增删改查询)

1、定义baseAction,存放结果码常量,请求、响应、上下文、公用的传值
2、Struts标签的使用
s:iterator
S:action
S:url
S:form
s:textfield
S:select
S:radio
S:param
s:textarea

1、不直接跳页面,跳子控制器,因为路径问题和*。action配置
2、修改页面弹栈的问题,load出的结果作为跟,属性可以直接取值
3、页面样式问题	theme

查询用转发 (值带到转发页面)
增删改用重定向(两次请求)
struts2带值 用ognl

demo(增删改查询)
web层
①BaseAction 一个通用的ation
package com.zking.four.web;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

/**

  • 每一个开发的子控制器要用的属性都定义在通用的action中。
  • @author Administrator

/
public class BaseAction implements ServletRequestAware, ServletResponseAware{
/
*
* 为了传值使用
*/
protected HttpServletResponse response;
protected HttpServletRequest request;
protected HttpSession session;
protected ServletContext application;

/**
 * 为了配置跳转页面所用
 */
protected final static String SUCCESS = "success";
protected final static String FAIL = "fail";
protected final static String LIST = "list";
protected final static String ADD = "add";
protected final static String EDIT = "edit";
protected final static String DETAIL = "detail";

/**
 * 具体传值字段	后端向jsp页面传值所用字段
 */
protected Object result;
protected Object msg;
protected int code;

public Object getResult() {
	return result;
}

public Object getMsg() {
	return msg;
}

public int getCode() {
	return code;
}

@Override
public void setServletResponse(HttpServletResponse arg0) {
	this.response = arg0;
	
}

@Override
public void setServletRequest(HttpServletRequest arg0) {
	this.request = arg0;
	this.session = arg0.getSession();
	this.application = arg0.getServletContext();
}

}

②StudentAction 进行增删改操作
package com.zking.four.web;

import java.sql.SQLException;

import com.opensymphony.xwork2.ModelDriven;
import com.zking.test.dao.StudentDAO;
import com.zking.test.entity.Student;
import com.zking.test.util.PageBean;

public class StudentAction extends BaseAction implements ModelDriven{
//犯错① 添加无法插入sid
//必须实现ModelDriven接口要不然无法将页面的值赋值到数据库中

StudentDAO studentDao=new StudentDAO();
Student student=new Student();
PageBean pageBean=new PageBean();

public String list() {
/**犯错② 页面无法出现分页效果
  //解决 pageBean.setRequest(request),忘记把request放进去页面就无法取出来
*/
	//把它放进request里面
	pageBean.setRequest(request);
	try {
		this.result=studentDao.list(student, pageBean);
		request.setAttribute("pageBean", pageBean);
		System.out.println("ssss");
	} catch (InstantiationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return LIST;
}

public String add() {
	try {
		this.studentDao.add(student);
	} catch (InstantiationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchFieldException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SecurityException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return SUCCESS;
}

public String toAdd() {
	return ADD;
}

public String edit() {
	try {
		this.studentDao.edit(student);
		this.msg="修改成功";
	} catch (InstantiationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchFieldException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SecurityException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return SUCCESS;
}

//跳转到修改页面的步骤
public String toEdit() {
	try {
		this.result=studentDao.load(student);
	} catch (InstantiationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return EDIT;
}

public String delete() {
	try {
		this.studentDao.del(student);
	} catch (InstantiationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchFieldException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SecurityException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return SUCCESS;
}

@Override
public Student getModel() {
	// TODO Auto-generated method stub
	return student;
}

}

③ClazzAction 进行页面的展示
package com.zking.four.web;

import java.sql.SQLException;

import com.zking.test.dao.ClazzDAO;

public class ClazzAction extends BaseAction{
public String execute() {
try {
//加载数据展示在页面
this.result=new ClazzDAO().list();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}

struts_sy.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<action name="clzAction" class="com.zking.four.web.ClazzAction">
</action> 

<action name="studentAction_*" class="com.zking.four.web.StudentAction" method="{1}">
 <result name="list">/jsp/listStudent.jsp</result>
 <result name="add">/jsp/addStudent.jsp</result>
 <result name="edit">/jsp/editStudent.jsp</result>
<!--为了增删改调回主页面 -->
 <result name="success" type="redirect">/sy/studentAction_list.action</result>
 
</action> 
</package>

jsp页面
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@ taglib uri="/struts-tags" prefix=“s”%>
<%@ taglib uri="/zking" prefix=“z”%>

Insert title here <%@ include file="/jsp/common/head.jsp" %>

list

新增
序号学号姓名拼音性别标记班级操作
序号
		<s:url namespace="/sy" action="studentAction_delete" var="toDelete">
		<!-- 需要带参数传值 -->
		<s:param name="sid" value="#s.sid"/>
		</s:url>
        <s:a href="%{#toEditUrl}">修改</s:a>
        <s:a href="%{#toDelete}">删除</s:a>
		</td>
	</s:iterator>
</table>
<z:page pageBean="${pageBean }"/>

editStudent.jsp修改
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>

<%@ include file="/jsp/common/head.jsp" %>

update

<%-- --%> ```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值