struts2 POJO
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="main" extends="struts-default" > <action name="*Book" class="com.helloweenvsfei.struts2.action.BookAction" method="{1}"> <result>/successBook.jsp</result> <result name="{1}">/{1}Book.jsp</result> <!-- <result name="input">/initAddBook.jsp</result> --> <result name="list">/listBook.jsp</result> </action> </package> </struts>
package com.helloweenvsfei.struts2.bean; import java.sql.Date; public class Book { private String name; private String author; private Date publishedDate; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Date getPublishedDate() { return publishedDate; } public void setPublishedDate(Date publishedDate) { this.publishedDate = publishedDate; } }
package com.helloweenvsfei.struts2.action; import java.util.ArrayList; import java.util.List; import com.helloweenvsfei.struts2.bean.Book; import com.opensymphony.xwork2.ActionSupport; //@Validation() public class BookAction extends ActionSupport { private static final long serialVersionUID = -38241432793476229L; public static List<Book> bookList = new ArrayList<Book>(); private String title; private Book book; // 添加书籍页面 public String initAdd() { return "initAdd"; } // 添加书籍 public String add() { bookList.add(book); title = "<br/><br/>添加书籍成功<br/><br/>"; return "success"; } // 书籍列表 public String list() { return "list"; } // 清空书籍列表 public String clear() { bookList.clear(); title = "<br/><br/>清空书籍列表成功<br/><br/>"; return "list"; } public Book getBook() { System.out.println("public Book getBook()"); return book; } public void setBook(Book book) { System.out.println("public void setBook(Book book)"); this.book = book; } public List<Book> getBookList() { System.out.println("public List<Book> getBookList()"); return bookList; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
initAddBook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="struts"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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">
<style type="text/css">
body, th, td {font-size: 12px; }
</style>
<struts:head theme="ajax" />
</head>
<body>
<a href="<struts:url action="initAddBook" />">添加书籍</a>
<a href="<struts:url action="listBook" />">书籍列表</a>
<a href="<struts:url action="clearBook" />">清空书籍列表</a>
<struts:form action="addBook" validate="true">
<struts:label value="添加书籍"></struts:label>
<struts:textfield name="book.name" label="书名"></struts:textfield>
<struts:textfield name="book.author" label="作者"></struts:textfield>
<struts:textfield name="book.publishedDate" label="出版日期"></struts:textfield>
<struts:submit value="添加"></struts:submit>
</struts:form>
</body>
</html>
listBook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="struts"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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">
<style type="text/css">
table {border-collapse: collapse; border: 1px solid #000000; margin-top: 20px; }
th, td {border: 1px solid #000000; font-size: 12px; }
body {font-size: 12px; }
</style>
</head>
<body>
<a href="<struts:url action="initAddBook" />">添加书籍</a>
<a href="<struts:url action="listBook" />">书籍列表</a>
<a href="<struts:url action="clearBook" />">清空书籍列表</a>
<struts:property value="title" escape="false"/>
<table>
<tr>
<th>书名</th>
<th>作者</th>
<th>出版日期</th>
</tr>
<struts:iterator id="book" value="bookList">
<tr>
<td>${ book.name }</td>
<td>${ book.author }</td>
<td>${ book.publishedDate }</td>
</tr>
</struts:iterator>
</table>
</body>
</html>
successBook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="struts"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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">
<style type="text/css">
body, th, td {font-size: 12px; }
</style>
<struts:head theme="ajax" />
</head>
<body>
<a href="<struts:url action="initAddBook" />">添加书籍</a>
<a href="<struts:url action="listBook" />">书籍列表</a>
<a href="<struts:url action="clearBook" />">清空书籍列表</a>
<br>
<h1>添加书籍成功</h1>
<br>
<struts:property value="title" escape="false"/>
</body>
</html>

浙公网安备 33010602011771号