1. 程式人生 > >Struts2入門到精通六——————ModelDriven

Struts2入門到精通六——————ModelDriven

一、

1、新建Struts2專案

2、org.zttc.itat.model包下建立Message.java

package org.zttc.itat.model;

public class Message {
	private int id;
	private String title;
	private String content;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	
	
}

3、org.zttc.itat.action 包下建立 MessageAction.java

package org.zttc.itat.action;

import org.zttc.itat.model.Message;

public class MessageAction {
	
	private Message msg;
	
	
	public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}

	public String addInput(){
		
		return "success";
	}
	
	public String add(){
		
		return "success";
	}

}

4、在WEB-INF中建立兩個JSP頁面

addInput.jsp如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Message AddInput</h1>

<s:debug/>

<form action="Message_add.action" method="post">

	Id:<input	type="text" name="id"/>	<br/>
	Title:<input	type="text" name="title"/>	<br/>
	Content:<input	type="text" name="content"/>	<br/>
	<input type="submit" />
</form>
</body>
</html>

add.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>
<h1>Message Add</h1>


${msg.id }---${msg.title}---${msg.content}
</body>
</html>

5、在瀏覽器中輸入http://localhost:8080/struts01_7/Message_addInput.action  並輸入表單:


點選提交,得到的頁面不會顯示  Id、Title、Content資訊。

為什麼呢??

把 addInput.jsp中的程式碼  改成如下:


即把id、title、content前面新增msg.     就可以得到資料:

Message Add


1---niaho---woshidahuilang

二、使用ModleDriven

1、讓MessageAction.java中的MessageAction實現ModelDriven介面

並實現getModel方法。

package org.zttc.itat.action;

import org.zttc.itat.model.Message;

import com.opensymphony.xwork2.ModelDriven;

public class MessageAction implements ModelDriven<Message>{
	
	private Message msg;
	
	
	public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}

	public String addInput(){
		
		return "success";
	}
	
	public String add(){
		
		return "success";
	}


	public Message getModel() {
		if(msg==null) msg= new Message();
		return msg;
	}
}

2、addInput改成如下  
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Message AddInput</h1>

<s:debug/>

<form action="Message_add.action" method="post">
<!-- 當使用ModelDriven的時候,Message物件放到root中的棧頂,MessageAction放到Message物件後面,
	所以使用id可以訪問到,使用msg.title也可以訪問到,因為msg.title首先在root棧頂找,找不到往後面(MessageAction)繼續找。
 -->
	Id:<input	type="text" name="msg.id"/>	<br/>
	Title:<input	type="text" name="msg.title"/>	<br/>
	Content:<input	type="text" name="msg.content"/>	<br/>
	<input type="submit" />
</form>
</body>
</html>

3、開啟瀏覽器,輸入http://localhost:8080/struts01_7/Message_addInput.action   並點選其中的Debug超連結,如下:


點選提交   會跳到http://localhost:8080/struts01_7/Message_add.action

結果 為:

Message Add


1---niaho---woshidahuilang

分析開啟的Debug,  我們發現在棧頂的是Message類,而一般都是Action在棧頂。使用了ModelDriven就會達到這種效果。

三、

1、MessageAction.java改變如下:

package org.zttc.itat.action;

import org.zttc.itat.dao.MessageDao;
import org.zttc.itat.model.Message;

import com.opensymphony.xwork2.ModelDriven;

public class MessageAction implements ModelDriven<Message>{
	
	private Message msg;
	
	
	public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}

	public String addInput(){
		
		return "success";
	}
	
	public String add(){
		
		return "success";
	}

	public String updateInput(){
		MessageDao md=new MessageDao();
		msg=md.load();
		return "success";
	}
	
	public Message getModel() {
		if(msg==null) msg= new Message();
		return msg;
	}
}

2、新建包org.zttc.itat.dao下面新建MessageDao.java
package org.zttc.itat.dao;

import org.zttc.itat.model.Message;

public class MessageDao {
	
	public Message load(){
		Message msg =new Message();
		msg.setId(12);
		msg.setContent("我是大白兔");
		msg.setTitle("小白兔");
		return msg;
		
	}
}

3、Message資料夾下面新建updateInput.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Message updateInput</h1>

<s:debug/>

<form action="Message_add.action" method="post">
	Id:<input	type="text" name="id" value=" <s:property value="id"/>"/>	<br/>
	Title:<input	type="text" name="title"  value=" <s:property value="title"/>"/>	<br/>
	Content:<input	type="text" name="content"   value=" <s:property value="content"/>"/>	<br/>
	<input type="submit" />
</form>
</body>
</html>




瀏覽器中輸入:http://localhost:8080/struts01_7/Message_updateInput.action  得到:


沒有值,為什麼????????

4.1

當把updateInput.JSP改動如下:


就可以訪問到:


4.2

或者   改動MessageAction.java  如下:


那麼就不需要改動 updateInput.jsp也可以訪問到。

4.3  再或者  新增commons-beanutils-1.8.3.jar commons-logging-1.1.1.jar這兩個jar包到lib

然後  改動MessageAction.java  如下: