<%@taglibprefix="s"uri="/struts-tags"%>作者李刚的图书作者李刚的图书

struts2中页面取值的原理

系统 1333 0
    <%@ page language="java" contentType="text/html; charset=GBK"%><!-- 导入Struts 2的标签库 --><%@taglib
	prefix="s" uri="/struts-tags"%><html>
	<head>
		<title>作者李刚的图书</title>
	</head>
	<body>
		<table border="1" width="360">
			<caption>
				作者李刚的图书
			</caption>
			<!-- 迭代输出ValueStack中的books对象,其中status是迭代的序号 -->
			<s:iterator value="books" status="index">
				<!-- 判断序号是否为奇数 -->
				<s:if test="#index.odd == true">
					<tr style="background-color: #cccccc">
				</s:if>
				<!-- 判断迭代元素的序号是否不为偶数 -->
				<s:else>
					<tr>
				</s:else>
				<td>
					书名:
				</td>
				<td>
					<s:property />
				</td>
				</tr>
			</s:iterator>
		</table>
	</body>
</html>

  

 Struts 2将所有属性值封装在struts.valueStack请求属性里,可以通过 request .getAttribute("struts.valueStack")获取。Action所有的属性都被封装到了ValueStack对象中,它类似于map,Action中的属性名可以理解为ValueStack中value的名字。
  可以通过valueStack.findValue("name")来取值

 

 

    public class BookService{
//以一个常量数组模拟了从持久存储设备(数据库)中取出的数据
private String[] books = new String[]{"Spring2.0宝典" ,"轻量级J2EE企业应用实战","基于J2EE的Ajax宝典","Struts,Spring,Hibernate整合开发"};
//业务逻辑方法,该方法返回全部图书public String[] getLeeBooks(){return books;
}
}    
  

 

    public class GetBooksAction implements Action{
//该属性并不用于封装用户请求参数,而用于封装Action需要输出到JSP页面信息private String[] books;
//books属性的setter方法
public void setBooks(String[] books){
this.books = books;
}
//books属性的getter方法
public String[] getBooks(){
return books;
}
//处理用户请求的execute方法
public String execute() throws Exception{
//获取Session中的user属性
String user = (String)ActionContext.getContext().getSession().get("user");
//如果user属性不为空,且该属性值为scott
if (user != null && user.equals("scott")){
//创建BookService实例
BookService bs = new BookService();
//将业务逻辑组件的返回值设置成该Action的属性
setBooks(bs.getLeeBooks());
return SUCCESS;
}else{
return LOGIN;
}
}
}   
  

 

    <%@ page language="java" contentType="text/html; charset=GBK"><% @page import="java.util.*,com.opensymphony.xwork2.util.*"%>
<html>
<head>
<title>作者李刚的图书</title>
</head>
<body>
<table border="1" width="360">
<caption>作者李刚的图书</caption>
<%//获取封装输出信息的ValueStack对象
ValueStack vs = (ValueStack)request.getAttribut("struts.valueStack");
//调用ValueStack的fineValue方法获取Action中的books属性值
String[] books = (String[])vs.findValue("books");
//迭代输出全部图书信息
for (String book : books){
%>
<tr>
<td>书名:</td>
<td><%=book%></td>
</tr>
<%}%>
</table>
</body>
</html>
  

  struts2中页面取值的原理

struts2中页面取值的原理


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论