1、定义baseAction,存放结果码常量,请求、响应、上下文、公用的传值
2、Struts标签的使用
s:iterator
S:action
S:url
S:form
s:textfield
S:select
S:radio
S:param
s:textarea
1、不直接跳页面,跳子控制器,因为路径问题和*。action配置
2、修改页面弹栈的问题,load出的结果作为跟,属性可以直接取值
3、页面样式问题 theme
查询用转发 (值带到转发页面)
增删改用重定向(两次请求)
struts2带值 用ognl
demo(增删改查询)
web层
①BaseAction 一个通用的ation
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();
}
}
②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{
//犯错① 添加无法插入sid
//必须实现ModelDriven接口要不然无法将页面的值赋值到数据库中
StudentDAO studentDao=new StudentDAO();
Student student=new Student();
PageBean pageBean=new PageBean();
public String list() {
/**犯错② 页面无法出现分页效果
//解决 pageBean.setRequest(request),忘记把request放进去页面就无法取出来
*/
//把它放进request里面
pageBean.setRequest(request);
try {
this.result=studentDao.list(student, pageBean);
request.setAttribute("pageBean", pageBean);
System.out.println("ssss");
} 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);
} 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);
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 toEdit() {
try {
this.result=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;
}
}
③ClazzAction 进行页面的展示
package com.zking.four.web;
import java.sql.SQLException;
import com.zking.test.dao.ClazzDAO;
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配置
<?xml version="1.0" encoding="UTF-8"?><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>
</package>
jsp页面
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@ taglib uri="/struts-tags" prefix=“s”%>
<%@ taglib uri="/zking" prefix=“z”%>
list
新增序号 | 学号 | 姓名 | 拼音 | 性别 | 标记 | 班级 | 操作 |
序号 |
|
editStudent.jsp修改
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>