struts2 简单的增删改查以及struts2标签的使用

1.导jar包 , 配置xml 自定义z标签 z.tld
在这里插入图片描述
2.准备 src文件 (Dao方法 entity实体类 util包 …)以及 conf下的 struts.xml等文件

在这里插入图片描述

准备工作做完了
3.定义BaseAction,存放结果码常量,请求、响应、上下文、公用的传值

 package com.zking.test.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 HttpServletRequest request;
protected HttpServletResponse response;
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的 (get)
 * 
 * 结果信息 (可能是字符串 ,类..)
 * 
 * 结果标识(eg.1失败 2.成功)	
 */
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
	this.response=arg0;
}

@Override
public void setServletRequest(HttpServletRequest arg0) {
	// TODO Auto-generated method stub
	this.request=arg0;
	this.session=arg0.getSession();
	this.application=arg0.getServletContext();
}

     }

4.写ClazzAction以及StudentAction 继承 BaseAction
在这里插入图片描述

StudentAction

 package com.zking.test.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 list() {
	
	pageBean.setRequest(request);
	try {
		this.result=this.studentDAO.list(student, pageBean);
		request.setAttribute("pageBean", 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;
}

public String toAdd() {
	return ADD;
}



public String edit() {
	try {
		this.studentDAO.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.studentDAO.load(student);
	} catch (InstantiationException | IllegalAccessException | SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return EDIT;
}


public String delete() {
	try {
		this.studentDAO.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;
}



  }

5.配置struts-sy.xml
重点 1、不直接跳页面,跳子控制器,因为路径问题和 *。action配置
增删改必须是重定向 type=“redirect” 否则报错

  <?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="clzAction" class="com.zking.test.web.ClazzAction"> </action>
<action name="studentAction_*" class="com.zking.test.web.StudentAction" method="{1}">
   <result name="list">/jsp/listStudent.jsp</result>
   <result name="success" type="redirect">/sy/studentAction_list.action</result>
   <result name="add">/jsp/addStudent.jsp</result>
   <result name="edit">/jsp/editStudent.jsp</result>

</action>

</package>
 </struts>

主页面

 <%@ 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>list</h1>
<s:action name="clzAction" var="clzList" namespace="/sy"></s:action>

  <s:form name="studentAction" namespace="/sy">
<s:textfield name="sname" label="姓名"></s:textfield>
<s:select name="cid" list="#clzList.result" label="班级" listKey="cid"   listValue="cname" 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="toDeleteURL">
   <s:param name="sid" value="#s.sid"></s:param>
</s:url>			
<s:a href="%{#toDeleteURL}">删除</s:a>	

		
		</td>
		
		
	</tr>
	</s:iterator>
</table>

  <s:debug></s:debug>

  </body>
 </html>

效果图
在这里插入图片描述

新增
在这里插入图片描述
效果图
在这里插入图片描述

修改

重点2、修改页面弹栈的问题,load出的结果作为根,属性可以直接取值
在这里插入图片描述
在这里插入图片描述
注:如果没有写push标签 弹栈 ,修改就获取不到值

重点 3、页面样式问题 theme
之前的都是默认的演示,改变样式要用theme属性 改为simple
定为全局的是在strust-base.xmlz中配置 ,就可以根据自己的喜好修改了
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值