struts2 簡單增刪查改
阿新 • • 發佈:2018-11-10
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