一、
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 如下:

本文介绍Struts2框架中ModelDriven特性的使用方法。包括创建项目、Message实体类及Action,实现表单输入和数据显示,并通过ModelDriven优化数据处理流程。
2820

被折叠的 条评论
为什么被折叠?



