struts2操做pojo之小工程struts2ActionPOJO

下面的源碼和操做步驟依據java web整合開發王者歸來第16章,16.7 Action中使用POJO:p464css

pojo:就是javabean的意思,下面就是struts2操做javabean代碼過程html

1.web.xml文件內容java

  web.xml文件內容是通用的過濾器配置web

<?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配置ajax

<?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代碼sql

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代碼apache

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代碼:app

<%@ 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>

  標籤說明:eclipse

 <s:form>:struts用於輸出表單的標籤,validate屬性爲true表示表單提交前對輸入數據進行驗證,從上面代碼,你會發現,其中沒有table,tr,td標籤,由於<s:form>會自動生成<table>標籤;jsp

<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.發佈以後的效果圖以下:

   

  首頁:添加書籍                      查詢列表              清空列表以後的頁面

相關文章
相關標籤/搜索