使用Struts2 的ValueStack,来实现数据显示在页面上
Model层的:
GetBooks.java
package practice.model;
public class BookService
{
private String[] books = new String[]
{
"Spring2.0宝典",
"化工大法",
"如来神掌",
"大话西游"
};
public String[] getLeeBooks()
{
return books;
}
}
Controller层的:
GetBooksAction.java
package practice.structs2;
import com.opensymphony.xwork2.ActionContext;
import practice.model.*;
public class GetBooksAction implements Action
{
private String[] books;
public void setBooks(String[] books)
{
this.books = books;
}
public String[] getBooks()
{
return books;
}
public String execute() throws Exception
{
String user = (String)ActionContext.getContext().getSession().get("user");
if(null != user && user.equals("hello"))
{
BookService bs = new BookService();
setBooks(bs.getLeeBooks());
return SUCCESS;
}
else
{
return LOGIN;
}
}
}
structs.xml 的配置:
<action name="GetBooks" class="practice.structs2.GetBooksAction">
<result name="login" >/login.jsp</result>
<result name="success" >/showBook.jsp</result>
</action>
showBook.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="com.opensymphony.xwork2.util.*" %>
<%
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>作者李刚的书</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>
<%
//在Struts中ValueStack 可以把Action中定义的变量,提取到页面上
ValueStack vs = (ValueStack)request.getAttribute("struts.valueStack");
String[] books = (String[])vs.findValue("books");
for(String book : books)
{
%>
<%= book %><br/>
<%
}
%>
</body>
</html>
使用控件改良后的showBook.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="com.opensymphony.xwork2.util.*" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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 'showBook2.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:iterator value="books" status="index">
<s:if test="#index.odd == true">
<div style="background-color:#c8c8c8;width=400px;" ><s:property/></div>
</s:if>
<s:else>
<div style="background-color:#fff;width=400px;" ><s:property/></div>
</s:else>
</s:iterator>
</body>
</html>
浙公网安备 33010602011771号