- 準備用來存資料的JavaBean
- 在Action中宣告一個JavaBean的屬性,並有getter跟setter
- 在form表單中使用OGNL語法對應到Action中的屬性
- 在展示層中使用OGNL取值
- 在struts中設定好對應關係
準備用來存資料的JavaBean
package bean;
import java.sql.Date;
public class Book implements java.io.Serializable {
private String name;
private Double price;
private Integer num;
private Date releaseDate;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public Date getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
}
在Action中宣告一個JavaBean的屬性,並有getter跟setter
package action;
import com.opensymphony.xwork2.ActionSupport;
import bean.Book;
public class BookAction2 extends ActionSupport {
private Book book;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public String addBook() {
// 暫省略業務處理程式碼
return SUCCESS;
}
}
在form表單中使用OGNL語法對應到Action中的屬性
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="Big5"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>addBook2.jsp</title>
<s:head theme="xhtml" /><!-- 預設 -->
<sx:head debug="false" cache="false" parseContent="false" compressed="false" extraLocales="zh-tw,en-us,ja,ko" />
</head>
<body onLoad="setValue()">
<!--●顯式錯誤訊息-->
<!-- 【使用傳統的Html標籤,無法顯式錯誤訊息,要依賴 <s:fielderror />標籤,也無法保持住原來輸入的資料】 -->
<!-- 【使用Struts2的表單UI標籤,可以顯式錯誤訊息,不用依賴 <s:fielderror />標籤,還可保持住原來輸入的資料】 -->
<s:fielderror cssStyle="color: blue" />
<!-- 【使用Struts2的表單UI標籤,可以顯式錯誤訊息,不用依賴 <s:fielderror />標籤,還可保持住原來輸入的資料】 -->
<h2> 新增書籍</h2>
<s:form action="bookAction2" namespace="/Book" >
<s:textfield name="book.name" label="書籍名稱" />
<s:textfield name="book.price" label="書籍價格" />
<s:textfield name="book.num" label="發行數量" />
<sx:datetimepicker type="date" name="book.releaseDate" label="發行日期" displayFormat="yyyy-MM-dd " id="picker1" language="zh-tw" required="true" cssStyle="background:cyan ; font-size:13.5px"/>
<s:submit value="送出" method="addBook"/>
</s:form>
<script type="text/javascript">
function setValue() {
var picker1 = dojo.widget.byId("picker1");
if (picker1.getValue() == "")
picker1.setValue(new Date()); //Date value //picker.setValue('2007-01-01'); //string value
}
</script>
</body>
</html>
在展示層中使用OGNL取值
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="Big5"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>addSuccess2.jsp</title>
<s:head theme="xhtml" /><!--預設-->
</head>
<body><br>
<UL>
<LI><h3>書籍名稱:<s:property value="book.name" /></h3></LI>
<LI><h3>書籍價格:<s:property value="book.price" /></h3></LI>
<LI><h3>發行數量:<s:property value="book.num" /></h3></LI>
<LI><h3>發行日期:<s:property value="book.releaseDate" /></h3></LI>
</UL>
</body>
</html>
在struts中設定好對應關係
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<!--以下設定全域範圍的國際化資源檔,檔名globalMessages起頭,如globalMessages_zh_TW.properties-->
<!--globalMessages名稱可自取 -->
<!--無預設值 -->
<constant name="struts.custom.i18n.resources" value="globalMessages" />
<package name="main" namespace="/Book" extends="struts-default">
<!-- 以下的 <global-results> 與 <global-exception-mappings> 適用於此 package 內所 acton -->
<!-- 在執行中如果出現 Exception 時會導向根目錄的 error.jsp, 然後由 error.jsp 顯示 500堆疊 -->
<!-- 此設定可有可無,不一定要設 -->
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings>
<action name="bookAction2" class="action.BookAction2" method="addBook">
<result name="success">/addSuccess2.jsp</result>
<result name="input">/addBook2.jsp</result>
</action>
</package>
</struts>