Struts2 CRUD

  1. 前台查询页面(studentList.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>Insert title here</title>
<%@  include file="/jsp/common/head.jsp"%>
</head>
<body>
<h1>学生</h1>
      <!--获取班级数据  -->
         <s:action name="clazzAction" namespace="/sy"  var="clazzActionList"></s:action> 

  
     <s:form namespace="/sy" action="studentAction_list">
            <s:textfield label="姓名" name="sname"></s:textfield>
            <s:select label="班级" name="cid"   list="#clazzActionList.result" listKey="cid" listValue="cname" headerKey="" headerValue="==选择=="></s:select>
           <s:submit value="提交"></s:submit>
     </s:form>
  
  <!-- 增加 -->
      <s:url namespace="/sy" action="studentAction_transitAdd" var="stuadd"></s:url>
				 <s:a href="%{#stuadd}">增加</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_transitEdit" var="stuedit">
			   <s:param name="sid" value="#s.sid"></s:param>
			 
			</s:url>
				 <s:a href="%{#stuedit}">修改</s:a>
				 
		    <s:url namespace="/sy" action="studentAction_delete" var="studelete">
		     <s:param name="sid" value="#s.sid"></s:param>
		     </s:url>
		    <s:a href="%{#studelete}">删除</s:a>
			</td> 
		</tr>
		</s:iterator>
	</table>
	<s:debug/>
</body>
</html>

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

3.修改页面(editStudent.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">
<%@ include file="/jsp/common/head.jsp" %>
</head>
<body>
	<h1>edit</h1>
	
	 <!--获取班级数据  -->
         <s:action name="clazzAction" namespace="/sy"  var="clazzActionList"></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 label="性别" name="sex" list="#{1:'男',2:'女'}" value="sex"/>
		<s:select label="班级" name="cid" headerKey="" headerValue="===请选择==="  list="#clazzActionList.result" listKey="cid" listValue="cname" cssStyle="width:160px;" />
		<s:textarea label="备注" name="mark"></s:textarea>
		<s:submit value="确定"/>
	</s:form>
	</s:push>    
	
	<s:debug/>
</body>
</html>

4.后台代码

package com.struts.web;

import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ModelDriven;
import com.strurts.utli.PageBean;
import com.struts.dao.StudentDAO;
import com.struts.entity.Student;

public class StudentAction extends BaseAction implements ModelDriven<Student>{
	
	private Student student=new Student();
	private StudentDAO studentDAO=new StudentDAO();
	/**
	 * 查询学生
	 * @return
	 */
	public String list() {
		
		try {
			PageBean pageBean=new PageBean();
			this.result=this.studentDAO.list(student, pageBean);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return LIST;
	}

	
	
	/**
	 * 增加学生
	 */
	public String add() {
		try {
			this.studentDAO.add(student);
		} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	
	
	/**
	 * 修改学生信息
	 * @return
	 */
	public String edit() {
		try {
			this.studentDAO.edit(student);
		} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 删除学生
	 * @return
	 */
	public String delete() {
		try {
			this.studentDAO.del(student);
		} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	
	/**
	 * 中转
	 * @return
	 */
	public String transitAdd() {
		
		
		return ADD;
	}
	
	
	/**
	 * 中转Edit
	 * @return
	 */
	public String transitEdit() {
		try {
			this.result=this.studentDAO.load(student);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return EDIT;
	}
	
	
	
	
	
	
	
	@Override
	public Student getModel() {
		// TODO Auto-generated method stub
		return student;
	}
	

}

6.后台Action继承于(BaseAction )这个类

package com.struts.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletResponse;
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;

import com.strurts.utli.PageBean;

public class BaseAction implements ServletRequestAware,ServletResponseAware{

	
	/**
	 * 域对象
	 */
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	protected ServletContext application;

	
	
	/**
	 * 结果码
	 */
	protected static final String SUCCESS ="success";//成功
	protected static final String FALI="fali";   //失败
	protected static final String ADD="add";   //增加
	protected static final String EDIT="edit";  //修改
	protected static final String DELETE="delete"; //删除
	protected static final String LIST="list";  //查询
	
	
	/**
	 * 传递的变量
	 * 结果信息
	 * 结果标识
	 */
	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) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
}

6.中转xml的配置(struts-sy.xml)

<?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>
	<package name="sy" extends="base" namespace="/sy">


<action name="usetAction_*" class="com.struts.web.UserAction" method="{1}">
 <result name="index">/index.jsp</result>
</action>


<!-- 班级 -->
 <action name="clazzAction" class="com.struts.web.ClazzAction" >
</action>

<!--  学生-->
 <action name="studentAction_*" class="com.struts.web.StudentAction"  method="{1}">
   <result name="list" >/jsp/studentList.jsp</result>
     <result name="success" type="redirect">/sy/studentAction_list.action</result>
     <result name="edit" >/jsp/editStudent.jsp</result>
     <result name="add" >/jsp/addStudent.jsp</result>
</action>

	</package>
</struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值