1. 程式人生 > >Struts-setter、getter方法自動 ActionSupport類-屬性驅動+模型驅動

Struts-setter、getter方法自動 ActionSupport類-屬性驅動+模型驅動

【setter、getter方法自動設定獲取】
屬於攔截器的功能


在TestAction類裡設定兩個屬性


bookId
bookName

並且聲稱setter、getter方法

public class TestAction implements Action {

	private int bookId;
	private String bookName;
	public int getBookId() {
		return bookId;
	}
	public void setBookId(int bookId) {
		this.bookId = bookId;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("execute()是預設執行的方法");
		return SUCCESS;
	}

}
然後在位址列傳入引數

在jsp頁面用EL表示式輸出
<body>
測試頁面<br>
${bookId }
${bookName }
</body>


【ActionSupport類】
Action比較侷限
實際一般都用ActionSupport類(更強大),它本身實現了Action介面。
繼承extends ActionSupport
package com.zhiqi.test;

import com.opensymphony.xwork2.ActionSupport;

public class TestActionSupport extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int bookId;
	private String bookName;
	public int getBookId() {
		return bookId;
	}
	public void setBookId(int bookId) {
		this.bookId = bookId;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("ActionSupport的execute()是預設執行的方法");
		return SUCCESS;
	}

}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="testAction" extends="struts-default">
		<action name="showMethod" class="com.zhiqi.test.TestActionSupport">
			<result name="success">test.jsp</result>		
		</action>
	</package>
</struts>


【設定屬性】屬性驅動(基本型別+JavaBean型別)+模型驅動
實現基本的登入驗證

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
登入驗證測試
<form action="login" method="post">
使用者名稱:<input type="text" name="userName" id="userName">
密 碼:<input type="password" name="password" id="password">
<input type="submit" value="登入">
</form>
</body>
</html>
通過配置xml交給對應的類處理
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="testAction" extends="struts-default">
		<action name="login" class="com.zhiqi.test.UserAction">
			<result name="success">success.jsp</result>
			<result name="error">error.jsp</result>	
		</action>
	</package>
</struts>
此處重點是,不再用getParameter()獲取引數,可以自動。
package com.zhiqi.test;

import com.opensymphony.xwork2.ActionSupport;
import com.zhiqi.model.User;
import com.zhiqi.service.UserService;

public class UserAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private String userName;
	private String password;
	private UserService userService=new UserService();
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("UserAction的預設方法");
		User user=new User();
		//替代了傳統的getParameter()
		user.setUserName(userName);
		user.setPassword(password);
		if(userService.login(user)){
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	
}

service層(.net人員稱之為商業邏輯層,實際開發還需加商業規則層用於驗證)
package com.zhiqi.service;

import com.zhiqi.model.User;

public class UserService {
	public boolean login(User user){
		if("web".equals(user.getUserName()) && "124".equals(user.getPassword())){
			return true;
		}else{
			return false;
		}
	}
}

model層
package com.zhiqi.model;

public class User {
	private String userName;
	private String password;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

【JavaBean型別】實際開發時model層有可能有幾十個欄位,那可以依次寫出,更好的是採用JavaBean


jsp頁面需要改動
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
登入驗證測試
<form action="login" method="post">
使用者名稱:<input type="text" name="user.userName" id="userName">
密 碼:<input type="password" name="user.password" id="password">
<input type="submit" value="登入">
</form>
</body>
</html>

通過xml交給對應的類
package com.zhiqi.test;

import com.opensymphony.xwork2.ActionSupport;
import com.zhiqi.model.User;
import com.zhiqi.service.UserService;

public class UserAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private User user;
	private UserService userService=new UserService();
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("UserAction的預設方法");
		if(userService.login(user)){//直接判斷
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	
}
JavaBean的處理更加的簡潔方便

【模型驅動】implements ModelDriven<User>
有缺陷、只能接受一個JavaBean
瞭解

jsp頁面提交

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
登入驗證測試
<form action="login" method="post">
使用者名稱:<input type="text" name="userName" id="userName">
密 碼:<input type="password" name="password" id="password">
<input type="submit" value="登入">
</form>
</body>
</html>
UserAction處理
package com.zhiqi.test;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zhiqi.model.User;
import com.zhiqi.service.UserService;

public class UserAction extends ActionSupport implements ModelDriven<User> {
	private static final long serialVersionUID = 1L;
	private User user=new User();//需要new
	private UserService userService=new UserService();
//	public User getUser() {
//		return user;
//	}
//	public void setUser(User user) {
//		this.user = user;
//	}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("UserAction的預設方法");
		if(userService.login(user)){//直接判斷
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	@Override
	public User getModel() {//自動設定一個JavaBean
		// TODO Auto-generated method stub
		return user;
	}
	
}

●checkbox傳入多個值,而且不固定

如jsp頁面
<form action="hobby" method="post">
愛好:<input type="checkbox" name="hobby" value="游泳">游泳
<input type="checkbox" name="hobby" value="登山">登山
<input type="checkbox" name="hobby" value="滑行">滑行
<input type="checkbox" name="hobby" value="讀書">讀書
<input type="checkbox" name="hobby" value="音樂">音樂
<input type="submit" value="確定">
</form>
通過struts.xml配置
<action name="hobby" class="com.zhiqi.test.HobbyAction">
	<result name="success">success.jsp</result>
</action>
傳入HobbyAction時用String[],即String陣列來處理
package com.zhiqi.test;

import com.opensymphony.xwork2.ActionSupport;

public class HobbyAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String [] hobby;
	public String[] getHobby() {
		return hobby;
	}
	public void setHobby(String[] hobby) {
		this.hobby = hobby;
	}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		for(String h:hobby){
			System.out.println(h);
		}
		return SUCCESS;
	}
	
}

從頁面傳入多個物件的屬性
如Book書的 名稱、作者、價格
<body>
<h3>輸入圖書資訊,要求可以輸入多個,不要一本一本的新增</h3>
<form action="addBooks" method="post">
	<table>
		<tr>
			<td>圖書名稱</td>
			<td>圖書作者</td>
			<td>圖書價格</td>
		</tr>
		<tr>
			<td><input type="text" name="books[0].bookName" /></td>
			<td><input type="text" name="books[0].bookAuthor" /></td>
			<td><input type="text" name="books[0].bookPrice" /></td>
		</tr>
		<tr>
			<td><input type="text" name="books[1].bookName" /></td>
			<td><input type="text" name="books[1].bookAuthor" /></td>
			<td><input type="text" name="books[1].bookPrice" /></td>
		</tr>
		<tr>
			<td><input type="submit" value="提交" /></td>
			<td></td>
			<td></td>
		</tr>
	</table>
</form>
</body>

<action name="addBooks" class="com.zhiqi.test.AddBooksAction">
	<result name="success">success.jsp</result>
</action>
此時Struts提供了集合,如用ArrayList<Book>來接收
package com.zhiqi.test;

import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;
import com.zhiqi.model.Book;

public class AddBooksAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<Book> books;
	public ArrayList<Book> getBooks() {
		return books;
	}
	public void setBooks(ArrayList<Book> books) {
		this.books = books;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		for(Book b:books){
			System.out.println(b);
		}
		return SUCCESS;
	}
	
}