Struts值栈与Ognl
生活随笔
收集整理的這篇文章主要介紹了
Struts值栈与Ognl
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
配置xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"> <struts><package name="stack" namespace="/stack" extends="struts-default"><action name="stack_*" class="star.july.c_valuestack.StackAction" method="{1}"><result name="success">/index.jsp</result><result name="input">/add.jsp</result></action></package></struts>
StackAction: ??
package star.july.c_valuestack; import java.util.Map; import star.july.b_validation.Student; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.ValueStack; public class StackAction extends ActionSupport{Student student;public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}public String stack(){String name = student.getName();ActionContext ac = ActionContext.getContext();//使用對象棧存儲對象//獲取值棧,用list的棧方式存取ValueStack vs = ac.getValueStack();//將對象放入棧中,壓棧vs.push(student);//彈棧// vs.pop();//使用映射棧存取對象//添加自己的值和棧Map map = ac.getContextMap();map.put("map", "自定義");//獲取requestMap rp = (Map)ac.get("request");rp.put("rp", "請求");//獲取sessionMap sp = ac.getSession();sp.put("sp", "會話");//獲取application并賦值Map ap = ac.getApplication();ap.put("ap", "應用");System.out.println(student);return SUCCESS;} }
輸入值:
<body><s:fielderror></s:fielderror><form action="<c:url value='/stack/stack_stack'/>" method="post">用戶名:<input type="text" name="student.name"><br><input type="submit" value="提交"/></form>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><!-- 對象棧的取方式,用[] --><!-- 取出方式,從下表開始一直取到結束為止 -->對象棧的取方式,用[]<s:property value="[1]"/><br><s:property value="[0]"/><br><br><br><!-- 映射棧的取值方式,前面加# -->映射棧的取值方式,前面加#<br><s:property value="#map"/><br><s:property value="#request.rp"/><br><s:property value="#session.sp"/><br><s:property value="#application.ap"/><!-- 調試值棧,看值 --><s:debug></s:debug></body> </html>
總結
以上是生活随笔為你收集整理的Struts值栈与Ognl的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Struts2注册信息验证判断是否符合格
- 下一篇: Ognl标签常用例子 只能在Struts