Struts2~获取狭义上的值栈

it2022-05-05  104

一,项目搭建 二,创建action层 具体代码如下:

package com.xiaogao.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.ValueStack; public class HelloAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; @Override public String execute() throws Exception { ActionContext actionContext = ActionContext.getContext(); //获取狭义上的值栈 ValueStack valueStack = actionContext.getValueStack(); valueStack.set("name","张三"); valueStack.set("age",11); return SUCCESS; } }

三,struts.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> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="manage" namespace="/" extends="struts-default"> <action name="hello" class="com.xiaogao.action.HelloAction"> <result name="success">success.jsp</result> </action> </package> </struts>

四,然后创建success.jsp文件 获取狭义上的值栈数据,具体如下

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> 获取狭义上的值栈数据:<s:property value="name"/> <s:property value="age"/> </body> </html>

ps: <%@taglib prefix=“s” uri="/struts-tags" %> 为引入struts2标签

五,运行 输入路径获取相应的数值。


最新回复(0)