struts值crud 增删查改

、定义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

共享界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html >
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="/zking" prefix="z"%>

struts-sy配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- namespace:在内存中划分具体的控件
	name:给package包取个名字 可以继承
 -->
	<package name="sy" extends="base" namespace="/sy">
	<action name="clazzAction" 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>
</struts>


助手BaseAction

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<Student>{
	private Student student=new Student();
	private StudentDAO studnetDAO=new StudentDAO();
	private PageBean pageBean=new PageBean();
	public String list() {
		try {
			this.result=studnetDAO.list(student,pageBean);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return LIST;
	}
	
	
	public String add() {
		try {
			this.studnetDAO.add(student);
//			this.msg="ssss";
		} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String toAdd() {
		return ADD;
	} 
	
	
	public String edit() {
		try {
			this.studnetDAO.edit(student);
		} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String toEdit() {
		try {
			this.result=this.studnetDAO.load(student);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return EDIT;
	}
	
	
	public String delete() {
		try {
			System.out.println(student.getSid());
			this.studnetDAO.del(student);
		} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
				| 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;

/**
 * 加载数据
 * @author Administrator
 *
 */
public class ClazzAction extends BaseAction{
	public String execute() {
		try {
			this.result=new ClazzDAO().list();
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}

展示

<%@ 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">
<%@ include file="/jsp/common/head.jsp" %>
</head>
<body>
	<h1>list</h1>
	<s:action name="clazzAction" namespace="/sy" var="clzlist"></s:action>
	<s:form namespace="/sy" action="studentAction_list">
		<s:textfield label="姓名" name="sname"/>
		<s:select label="班级"  name="cid" headrKey="" headerValue="===请选择===" list="#clzlist.result" listKey="cid" listValue="cname"></s:select>
		<s:submit value="ok"/>
	</s:form>
	<s:url namespace="/sy" action="studentAction_toAdd" var="toAddUrl"></s:url>
	<s:a href="%{#toAddUrl}">新增</s:a>
	<table border="1" width="100%">
		<tr>
			<td>序号</td>
			<td>学号</td>
			<td>姓名</td>
			<td>拼音</td>
			<td>性别</td>
			<td>标记</td>
			<td>班级</td>
			<td>操作</td>
		</tr>
		<s:iterator var="s" value="result">
		<tr>
			<td>序号</td>
			<td><s:property value="#s.sid"/></td>
			<td><s:property value="#s.sname"/></td>
			<td><s:property value="#s.spin"/></td>
			<td><s:property value="#s.sex"/></td>
			<td><s:property value="#s.mark"/></td>
			<td><s:property value="#s.cname"/></td>
			<td>
				<s:url namespace="/sy" action="studentAction_toEdit" var="toEditUrl">
					<s:param name="sid" value="#s.sid"/>
				</s:url>
					
				<s:url namespace="/sy" action="studentAction_delete" var="toDelUrl">
					<s:param name="sid" value="#s.sid"/>
				</s:url>
					
				<s:a href="%{#toEditUrl}">修改</s:a>
				<s:a href="%{#toDelUrl}">删除</s:a>
			</td>
		</tr>
		</s:iterator>
	</table>
</body>
</html>

增加

<%@ 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">
<%@ include file="/jsp/common/head.jsp" %>
</head>
<body>
	<h1>add</h1>
	<s:action name="clazzAction" namespace="/sy" var="clzlist"></s:action>
	<s:form namespace="/sy" action="studentAction_add">
		<%-- <s:hidden name="sid" value="5"/> --%>
		<s:textfield label="学号" name="sid" />
		<s:textfield label="姓名" name="sname" />
		<s:radio label="性别" name="sex" list="{'男','女'}"/>
		<s:select label="班级" name="cid" headerKey="" headerValue="===请选择===" list="#clzlist.result" listKey="cid" listValue="cname" cssStyle="width:160px;" />
		<s:textarea label="备注" name="mark"></s:textarea>
		<s:submit value="确定"/>
	</s:form>
</body>
</html>

修改

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ include file="/jsp/common/head.jsp" %>
<!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>
	<h1>update</h1>
	<s:action name="clazzAction" namespace="/sy" var="clzlist"></s:action>
	<s:push value="result">
		<s:form namespace="/sy" action="studentAction_edit">
			<%-- <s:hidden name="sid" value="5"/> --%>
			<s:textfield label="学号" name="sid" />
			<s:textfield label="姓名" name="sname" />
			<s:radio label="性别" name="sex" list="{'男','女'}"/>
			<s:select label="班级" name="cid" headerKey="" headerValue="===请选择===" list="#clzlist.result" listKey="cid" listValue="cname" cssStyle="width:160px;" />
			<s:textarea label="备注" name="mark"></s:textarea>
			<s:submit value="确定"/>
		</s:form>
	</s:push>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/cg_9647/article/details/83053732
今日推荐