注:entity,dao,util包的可用自己写的。
定义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();
}
}
package com.zking.four.web;
import java.sql.SQLException;
import com.zking.four.web.dao.ClazzDAO;
/**
* 为了加载select框的数据
* @author Administrator
*
*/
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):
<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>
web.xml:
<filter-mapping>
<filter-name>struts</filter-name>
<!--在实际开发中,是*.action 只过滤action结尾的请求 -->
<url-pattern>*.action</url-pattern>
</filter-mapping>
主页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="/zking" prefix="z"%>
<!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" namespace="/sy" var="clzList"></s:action>
<s:form namespace="/sy" action="studentAction_list">
<s:textfield label="姓名" name="sname"/>
<s:select label="班级" name="cid" headerKey="" 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: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>
<z:page pageBean="${pageBean }"/>
</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="clzAction" 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"></s:select>
<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"%>
<!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>update</h1>
<s:action name="clzAction" namespace="/sy" var="clzList"></s:action>
<s:push value="result">
<s:form namespace="/sy" action="studentAction_edit" ><!-- theme="simple" 改样式 -->
<%-- <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"></s:select>
<s:textarea label="备注" name="mark"></s:textarea>
<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.four.web.dao.StudentDAO;
import com.zking.four.web.entity.Student;
import com.zking.four.web.util.PageBean;
public class StudentAction extends BaseAction implements ModelDriven<Student>{
Student student=new Student();
StudentDAO studentDao=new StudentDAO();
PageBean pageBean=new PageBean();
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
public String list(){
this.pageBean.setRequest(request);
try {
this.result=this.studentDao.list(student, pageBean);
request.setAttribute("pageBean", 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 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 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 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 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;
}
}
界面展示:
注:
1、不直接跳页面,跳子控制器,因为路径问题和*。action配置
2、修改页面弹栈的问题,load出的结果作为根,属性可以直接取值
3、页面样式问题 theme
4、pageBean 是我所写的分页。