请求数 地址栏
转发 1 不变
重定向 2 变
struts.xml的配置文件里面:
转发格式:<result name="list">/WEB-INF/jsp/roleAction/list.jsp</result>
重定向格式:<result name="toList" type="redirectAction">roleAction_list</result>
(功能:拿到数据,转到页面,显示数据)、
知识点:
redirectAction结果类型的实现类是org.apache.struts2.dispatcher.ServletActionRedirectResult,该类是
ServletDispatcherResult的子类,因此我们也就可以判断出redirectAction结 果类型和redirect结果类型
的后台工作原理是一样的,即都是利用HttpServletResponse的sendRedirect方法将请求重定向到指定的URL。
那么redirect与redirectAction有什么区别呢?
从结果类型的名字上,我们可以大致判断出redirectAction结果类型主要是用
于重定向到action。也就是说,在请求处理完成后,如果你需要重定向到另一
个action,那么建议你使用redirectAction结果类型。
作用 方法名 返回值 页面
---------------------------------------------
列表 list() list list.jsp
删除 delete() toList
添加页面 addUI() addUI addUI.jsp
添加 add() toList
修改页面 editUI() editUI editUI.jsp
修改 edit() toList
struts.xml的配置文件里面,有对应的页面
<!-- 岗位管理 -->
<action name="roleAction_*" class="roleAction" method="{1}">
<result name="list">/WEB-INF/jsp/roleAction/list.jsp</result>
<result name="addUI">/WEB-INF/jsp/roleAction/addUI.jsp</result>
<result name="editUI">/WEB-INF/jsp/roleAction/editUI.jsp</result>
<result name="setPrivilegeUI">/WEB-INF/jsp/roleAction/setPrivilegeUI.jsp</result>
<result name="toList" type="redirectAction">roleAction_list</result>
</action>
在action里面怎么得到传过来的值,应该从action里面定义一个long型的id,因为action默认是栈顶的。
OGNL表达式,因为这在strut2的自定义标签里面,在外面才能写ER表达式,里面只能写OGNL表达式,因为它是strut2来解析的
(四)、实现添加和添加页面
先写addUI.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<--这是struts的标签-->
<html>
<head>
<title>添加页面</title>
</head>
<body>
<s:form action="roleAction_add">
<s:textfield name="name" label="名称"></s:textfield>
<s:textarea name="description" label="说明"></s:textarea>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
知识点:一行用textfield,多行用textarea
RoleAction.java中,
为了得到页面的值,先定义两个值:
private String name;
private String description;(get set方法)
/** 添加 */
public String add() throws Exception {
// // 得到参数,封装成对象
// Role role=new Role();
// role.setName(model.getName());
// role.setDescription(model.getDescription());
//当使用实体做Model时,也可以直接使用model
// 保存到数据库中
roleService.save(model);
list.jsp中加入:
<s:a action="roleAction_addUI">添加</s:a>
(五)、实现修改
list.jsp中:
<s:a action ="roleAction_editUI?id=%{id}">修改</s:a>
editUI.jsp中:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<--这是struts的标签-->
<html>
<head>
<title>修改页面</title>
</head>
<body>
<s:form action="roleAction_add">
<s:hidden name="id"></s:hidden>
<s:textfield name="name" label="名称"></s:textfield>
<s:textarea name="description" label="说明"></s:textarea>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
因为显示到editUI页面的时候,要有显示的数据:
/** 修改页面 */
public String editUI() throws Exception {
//根据id拿到Role对象,根据id得到role对象
Role role=roleService.getById(model.getId());
//让role的信息显示到表单上,可以把name和description封装下
//让它可以回显在jsp界面上
// this.name=role.getName();
// this.description=role.getDescription();
ActionContext.getContext().getValueStack().push(role);//放到栈顶
return "editUI";
}