标签:
一:简单属性,对象,嵌套对象,指定域作value:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 简单属性作value:<br> 12 用户名:<s:property value="username"/><br> 13 密码:<s:property value="password"/><br> 14 对象作value:<br> 15 用户名:<s:property value="user.username"/><br> 16 密码:<s:property value="user.password"/><br> 17 嵌套对象作value:<br> 18 <s:property value="cat.friend.name"/> And <s:property value="cat.name"/>是好朋友!<br> 19 获取指定域的值:<br> 20 指定域session中的值:<s:property value="#session.USER"/> 21 </body> 22 </html>
二:需要有对应的Set,Get方法
nuc.sw.vo---> User.java Cat.java Mouse.java
1 package nuc.sw.vo; 2 3 public class User { 4 private String username; 5 private String password; 6 public String getUsername() { 7 return username; 8 } 9 public void setUsername(String username) { 10 this.username = username; 11 } 12 public String getPassword() { 13 return password; 14 } 15 public void setPassword(String password) { 16 this.password = password; 17 } 18 19 20 21 }
1 package nuc.sw.vo; 2 3 public class Cat { 4 private Mouse friend; 5 private String name; 6 public Mouse getFriend() { 7 return friend; 8 } 9 public void setFriend(Mouse friend) { 10 this.friend = friend; 11 } 12 public String getName() { 13 return name; 14 } 15 public void setName(String name) { 16 this.name = name; 17 } 18 19 }
1 package nuc.sw.vo; 2 3 public class Mouse { 4 private String name; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 }
三:action中做相应的set,get
nuc.sw.action--->OgnlAction.java
1 package nuc.sw.action; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionSupport; 5 6 import nuc.sw.vo.Cat; 7 import nuc.sw.vo.User; 8 9 public class OgnlAction extends ActionSupport { 10 private String username; 11 private String password; 12 13 public String getUsername() { 14 return username; 15 } 16 public void setUsername(String username) { 17 this.username = username; 18 } 19 public String getPassword() { 20 return password; 21 } 22 public void setPassword(String password) { 23 this.password = password; 24 } 25 26 27 private User user; 28 public User getUser() { 29 return user; 30 } 31 public void setUser(User user) { 32 this.user = user; 33 } 34 35 36 private Cat cat; 37 38 public Cat getCat() { 39 return cat; 40 } 41 public void setCat(Cat cat) { 42 this.cat = cat; 43 } 44 45 46 @Override 47 public String execute() throws Exception { 48 ActionContext.getContext().getSession().put("USER", "ZD"); 49 return SUCCESS; 50 } 51 }
四:对集合做相应的操作
(1) 循环和迭代</s:iterator>取数组的值,
(2)练习使用 ?(全部) ^(第一) $(最后一个),
(3)练习使用Status属性对奇数行或者偶数行做相应改变
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <s:property value="books"/> 12 <table border="1"> 13 <tr> 14 <td>书名</td> 15 <td>作者</td> 16 <td>价格</td> 17 </tr> 18 <tr> 19 <td><s:property value="books[0].bookName"/></td> 20 <td><s:property value="books[0].bookAuthor"/></td> 21 <td><s:property value="books[0].bookPrice"/></td> 22 </tr> 23 <tr> 24 <td><s:property value="books[1].bookName"/></td> 25 <td><s:property value="books[1].bookAuthor"/></td> 26 <td><s:property value="books[1].bookPrice"/></td> 27 </tr> 28 <tr> 29 <td><s:property value="books[2].bookName"/></td> 30 <td><s:property value="books[2].bookAuthor"/></td> 31 <td><s:property value="books[2].bookPrice"/></td> 32 </tr> 33 </table> 34 35 <table border="1"> 36 <s:iterator value="books" var="book"> 37 <tr> 38 <td><s:property value="#book.bookName"/></td> 39 <td><s:property value="#book.bookAuthor"/></td> 40 <td><s:property value="#book.bookPrice"/></td> 41 </tr> 42 </s:iterator> 43 </table> 44 <br/> 45 练习使用 ?(全部) ^(第一) $(最后一个)<br> 46 <s:property value="books.{?#this.bookPrice<50}.{bookName}"/><br> 47 <s:property value="books.{^#this.bookPrice<50}.{bookName}"/><br> 48 <s:property value="books.{$#this.bookPrice<50}.{bookName}"/><br> 49 50 练习使用Status属性奇数行做相应改变<br> 51 <table border="1"> 52 <tr> 53 <td>书名</td> 54 <td>作者</td> 55 <td>价格</td> 56 </tr> 57 <s:iterator value="books" var="book" status="st"> 58 <tr <s:if test="#st.odd">style="background-color:#ff0000"</s:if>> 59 <td><s:property value="#book.bookName"/></td> 60 <td><s:property value="#book.bookAuthor"/></td> 61 <td><s:property value="#book.bookPrice"/></td> 62 </tr> 63 </s:iterator> 64 </table> 65 <br/> 66 67 练习使用Status属性偶数行做相应改变<br> 68 <table border="1"> 69 <tr> 70 <td>书名</td> 71 <td>作者</td> 72 <td>价格</td> 73 </tr> 74 <s:iterator value="books" var="book" status="st"> 75 <tr <s:if test="#st.even">style="background-color:yellow"</s:if>> 76 <td><s:property value="#book.bookName"/></td> 77 <td><s:property value="#book.bookAuthor"/></td> 78 <td><s:property value="#book.bookPrice"/></td> 79 </tr> 80 </s:iterator> 81 </table> 82 </body> 83 </html>
五:在对应的action中添加书籍信息
1 package nuc.sw.action; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.opensymphony.xwork2.ActionSupport; 7 8 import nuc.sw.vo.Book; 9 10 public class OgnlListAction extends ActionSupport { 11 private List<Book> books=new ArrayList<Book>(); 12 13 public List<Book> getBooks() { 14 return books; 15 } 16 17 public void setBooks(List<Book> books) { 18 this.books = books; 19 } 20 public String addBook() throws Exception { 21 // TODO Auto-generated method stub 22 books.add(new Book("数据结构","严蔚敏",32.5f)); 23 books.add(new Book("数据库","贾美丽",13.2f)); 24 books.add(new Book("Struts","李刚",79.5f)); 25 return SUCCESS; 26 } 27 }
六:配置struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 9 <constant name="struts.devMode" value="true" /> 10 11 <package name="default" namespace="/" extends="struts-default"> 12 <action name="OgnlAction" class="nuc.sw.action.OgnlAction"> 13 <result>/hello.jsp</result> 14 </action> 15 <action name="BookListAction" class="nuc.sw.action.OgnlListAction" method="addBook"> 16 <result>/showBook.jsp</result> 17 </action> 18 </package> 19 20 21 </struts>
七:项目结构
八:运行结果
九:注意事项
(1):OgnlListAction中不能写 ,因为没有返回值return,所以在struts.xml中不能写method,因此会出错。
1 //public OgnlListAction(List<Book> books) { 2 // super(); 3 // this.books = books; 4 // //books.add(new Book("数据结构","严蔚敏",(float)32.5)); 5 //}
(2)<s:iterator>和<table>要有先后顺序,否则只迭代一个。
(3)背景颜色 background-color。
标签:
原文地址:http://www.cnblogs.com/Z-D-/p/5982832.html