欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Struts值栈与Ognl

发布时间:2025/3/20 编程问答 25 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。