struts2的简单增删查改

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

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 DETALL="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 setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request=arg0;
		this.session=arg0.getSession();
		this.application=arg0.getServletContext();
	}
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.response=arg0;
	}
}

2、Struts标签的使用
s:iterator
S:action
S:url
S:form
s:textfield
S:select
S:radio
S:param
s:textarea

//listStudent.jsp


<%@ 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>
<s:action name="clzAction" namespace="/sy" var="clzList"></s:action>
<s:form namespace="/sy" action="studentAction_list">
	<s:textfield label="姓名" name="sname"></s:textfield>
	<s:select label="班级" name="cid" listKey="cid" listValue="cname" list="#clzList.result" headerKey="" headerValue="全部"></s:select>
	<s:submit value="ok"></s:submit>
</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:param>
			</s:url>
			<s:a href="%{toEditUrl}">修改</s:a>
			<s:url namespace="/sy" action="studentAction_delete" var="toDelUrl">
			<s:param name="sid" value="#s.sid"></s:param>
			</s:url>
			<s:a href="%{toDelUrl}">删除</s:a>
			</td>
		</tr>
		</s:iterator>
	</table>
	<z:page pageBean="${pageBean }"/>
</body>
</html>
//addStudent.jsp

<%@ 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>
	<s:action name="clzAction" namespace="/sy" var="clzList"></s:action>
	<s:form namespace="/sy" action="studentAction_add">
	<s:textfield label="学号" name="sid"/>
	<s:textfield label="姓名" name="sname"/>
	<s:radio list="#{1:'男',2:'女'}" name="sex" label="性别"/>
	<s:select name="cid" label="班级" listKey="cid" listValue="cname" headerKey="" headerValue="--请选择---"  list="#clzList.result"></s:select>
	<s:textarea label="备注" name="mark"/>
	<s:submit value="确定"/>
	</s:form>
</body>
</html>
//editStudent.jsp


<%@ 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>
	
	<s:action name="clzAction" namespace="/sy" var="clzList"></s:action>
	<s:push value="result">
	<s:form namespace="/sy" action="studentAction_edit">
	<s:textfield label="学号" name="sid"/>
	<s:textfield label="姓名" name="sname"/>
	<s:radio list="#{1:'男',2:'女'}" name="sex" label="性别"/>
	<s:select name="cid" label="班级" listKey="cid" listValue="cname" headerKey="" headerValue="--请选择---"  list="#clzList.result"></s:select>
	<s:textarea label="备注" name="mark"/>
	<s:submit value="确定"/>
	</s:form>
	</s:push>
</body>
</html>
//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 studentDAO=new StudentDAO();
	private PageBean pageBean=new PageBean();
	
	
	public String add() {
		try {
			this.studentDAO.add(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 toAdd() {
		return ADD;
	}
	
	
	public String toEdit() {
		try {
			this.result=this.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 edit() {
		try {
			this.studentDAO.edit(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 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;
	}
	
	
	
	
	public String list() {
		try {
			pageBean.setRequest(request);
			this.result=studentDAO.list(student, pageBean);
		} 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 PageBean getPageBean() {
		return pageBean;
	}


	


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

//ClzAction


package com.zking.four.web;

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

import com.zking.test.dao.ClazzDAO;
import com.zking.test.entity.Clazz;

public class ClzAction extends BaseAction{
	private Clazz clz=new Clazz();
	private ClazzDAO clazzDAO =new ClazzDAO();
	
	public String execute() {
		try {
			this.result=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;
		
	}
	
}

//配置


<action  name="clzAction" class="com.zking.four.web.ClzAction">
		</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>

注意:

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

猜你喜欢

转载自blog.csdn.net/LJD_2001/article/details/83053753
今日推荐