下面的源码和操作步骤依据java web整合开发王者归来第16章,16.7 Action中使用POJO:p464
pojo:就是javabean的意思,下面就是struts2操作javabean代码过程
1.web.xml文件内容
web.xml文件内容是通用的过滤器配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name> <filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
2.struts.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts> <package name="main" extends="struts-default">
<action name="*Book" class="com.zyf.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> //疑点:这条语句为什么不能去掉,因为有上面的name="{1}",按照语法不是已经包括这条了么?实验过程中,如果没这条就会错误。 </action> </package> </struts>
3.action之BookAction.java代码
package com.zyf.action; import java.util.ArrayList;
import java.util.List; import com.opensymphony.xwork2.ActionSupport;
import com.zyf.bean.Book; public class BookAction extends ActionSupport{
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); //这个地方的book,按照java的基础语法,应该是不能直接使用的,因为book可能是空的,书上所说,struts2会在运行过程中通过反射创建一个对象,因此不会抛出NullPointException.
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 List<Book> getBookList() {
return bookList;
} //疑点,BookList是static修饰的,如果用myeclipse自动生成的话,方法前面会加上static,但是如果有static的话,数据不能保存下来,我在页面点查询列表就插叙不到结果,这个地方疑问,加了static有什么不一样的地方么?
public void setBookList(List<Book> bookList) {
BookAction.bookList = bookList;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public Book getBook() {
return book;
} public void setBook(Book book) {
this.book = book;
}
}
4.javabean之Book.java代码
package com.zyf.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;
} }
5.首页面之initAddBook.jsp代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="/struts-dojo-tags" prefix="sx" %> //这个地方新添加了一个标签库,因为书本上的struts2版本可能早了,如果这个地方不修改,下面还使用ajax的主题的话,会出错。使用这个标签库,需要添加jar,我用的是:struts2-dojo-plugin-2.1.8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <sx:head/> //注:这个地方使用的标签就不是struts2的标签了 <base href="<%=basePath%>"> <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>
<a href="<s:url action="initAddBook"/>">添加书籍</a>
<a href="<s:url action="listBook"/>">书籍列表</a>
<a href="<s:url action="clearBook"/>">清除书籍</a> <s:form action="addBook" validate="ture">
<s:label value="添加书籍"></s:label>
<s:textfield name="book.name" label="书名"></s:textfield>
<s:textfield name="book.author" label="作者"></s:textfield>
<s:textfield name="book.publishedDate" label="出版日期"></s:textfield>
<s:submit value="添加"></s:submit> </s:form> </body>
</html>
标签说明:
<s:form>:struts用于输出表单的标签,validate属性为true表示表单提交前对输入数据进行验证,从上面代码,你会发现,其中没有table,tr,td标签,因为<s:form>会自动生成<table>标签;
<s:textfield>标签输出一个HTML单行文本输入控件,等价于HTML代码<input type=”text”>,name对应于<input name>,而label对应于<input/>输入框前面的提示
<s:submit>标签即html中对应的type=submit,用于提交
6.成功页面之successBook.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<a href="<s:url action="initAddBook" />">添加书籍</a>
<a href="<s:url action="listBook" />">书籍列表</a>
<a href="<s:url action="clearBook" />">清空书籍列表</a> <s:property value="title" escape="false"/>
</body>
</html>
<s:url>标签一般和超链接 <a>一起使用,用于带多个参数,用于指定要处理的action,后面还有param,includeParams,includeContext ,Method ,namespace等属性,但是一般不用,关于<s:url>标签详细使用方法,可以参看http://www.cnblogs.com/zyf2013/p/3439289.html或者http://www.cnblogs.com/yaowen/archive/2013/02/21/2920596.html
<s:property>标签的escape属性默认值为true,即不解析html代码,直接将其输出。若想要输出html的效果,则要改为false
关于不解析html代码(理解有点小困难,s:property就是输出用的,解析不解析有区别么?难道value里面会出现很多html代码?)
7.查询页面之listBook.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title><s:property value="title" escape="false"/></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">
--> <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="<s:url action="initAddBook" />">添加书籍</a>
<a href="<s:url action="listBook" />">书籍列表</a>
<a href="<s:url action="clearBook" />">清空书籍列表</a> <s:property value="title" escape="false"/>
<table>
<tr>
<th>书名</th>
<th>作者</th>
<th>出版日期</th>
</tr>
<s:iterator id="book" value="bookList">
<tr>
<td>${book.name }</td>
<td>${book.author }</td>
<td>${book.publishedDate }</td> </tr> </s:iterator> </table>
</body>
</html>
下面是我找到的iterator标签的相关:
<s:iterator>迭代标签:用于遍历集合(java.util.Collection)或者枚举值(java.util.Iterator)类型的对象,value属性表示集合或枚举对象,status属性表示当前循环的对象,在循环体内部可以引用该对象的属性
<s:iterator value="userList" status="user">
姓名:<s:property value="user.userName" />
年龄:<s:property value="user.age" />
</s:iterator>
不过上面所说的和程序中用到的不相符呀。
8.发布之后的效果图如下:
首页:添加书籍 查询列表 清空列表之后的页面