Struts中通過前台頁面傳遞參數到后台,一般常用的有兩種方式。
一種是通過在Action處理類聲明屬性並提供SET/GET方法,另一種是Action處理類實現ModelDriven類並提供一個對應的POJO供Struts進行封裝。
一、通過聲明屬性的方式傳遞參數
Action處理類如下:
1 package com.basestruts.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 /** 5 * 通過直接在Action中聲明參數進行參數傳遞 6 * @author chenyr 7 * 8 */ 9 10 public class AS_SayHelloAction extends ActionSupport { 11 12 /** 13 * 版本號 14 */ 15 private static final long serialVersionUID = -4693323245030734260L; 16 17 /** 名字 **/ 18 private String name; 19 20 /** 消息 **/ 21 private String message; 22 23 24 /** 傳遞到前台的消息(必須提供SET/GET方法) **/ 25 private String sayHelloStr = ""; 26 27 public String execute() 28 { 29 sayHelloStr = "讀取不到前台消息!"; 30 31 if(name != null && message != null) 32 { 33 sayHelloStr = name + "," + message; 34 } 35 36 System.out.println(sayHelloStr); 37 38 return SUCCESS; 39 } 40 /* 省略name, message, sayHelloStr的SET/GET方法 */ 41 }
struts.xml文件如下:
1 <?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"> 2 <struts> 3 <package name="default" namespace="/" extends="struts-default"> 4 <action name="AS_sayHello" class="com.basestruts.action.AS_SayHelloAction" 5 method="execute"> 6 <result name="success">/result.jsp</result> 7 <result name="input">/index.jsp</result> 8 </action> 9 10 <action name="MD_sayHello" class="com.basestruts.action.MD_SayHelloAction" 11 method="execute"> 12 <result name="success">/result.jsp</result> 13 <result name="input">/index.jsp</result> 14 </action> 15 </package> 16 </struts>
前台頁面index.jsp用表單傳遞name, message參數:
1 <%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ page contentType="text/html;charset=utf-8" %> 3 4 <!-- 省略了其他代碼 --> 5 <body> 6 <%-- 通過Struts傳遞參數,數據傳遞方式必須選擇post,否則編碼錯誤! --%> 7 <form action="message/MD_sayHello" method="post" > 8 <b>名字:</b><input type="text" id="name" name="name" /></br> 9 <b>消息:</b><input type="text" id="message" name="message"/></br> 10 <input type="submit" name="submit" value="問好"/> 11 </form> 12 </body>
注意:Struts傳遞數據時,必須將表單的method屬性設為post,否則會出現編碼錯誤!
結果頁面result.jsp:
1 <body> 2 <h1>${sayHelloStr }</h1> 3 </body>
二、通過ModelDriven接口實現參數傳遞
此時的前后台頁面和strut.xml文件都不變,Action處理類修改如下:
1 package com.basestruts.action; 2 3 import com.basestruts.pojo.Message; 4 import com.opensymphony.xwork2.ActionSupport; 5 import com.opensymphony.xwork2.ModelDriven; 6 7 /** 8 * 通過ModelDriven方式傳遞參數 9 * @author chenyr 10 * 11 */ 12 public class MD_SayHelloAction extends ActionSupport implements ModelDriven<Message> { 13 14 /** 15 * 版本號 16 */ 17 private static final long serialVersionUID = -4693323245030734260L; 18 19 /** 聲明存儲前台信息的POJO類對象(不必提供GET/SET方法) **/ 20 private Message model = new Message(); 21 22 /** 傳遞到前台的消息(必須提供SET/GET方法) **/ 23 private String sayHelloStr = ""; 24 25 public Message getModel() { 26 // TODO Auto-generated method stub 27 return model; 28 } 29 30 public String execute() 31 { 32 setSayHelloStr("讀取不到前台消息!"); 33 /* 獲取前台的消息 */ 34 if(model != null) 35 { 36 Message message = (Message)getModel(); 37 setSayHelloStr(message.getName() + "," + message.getMessage()); 38 } 39 40 return SUCCESS; 41 } 42 43 /* 省略sayHelloStr的SET/GET方法 */ 44 } 45
這時相比第一種方式,Action的主要改變是提供了20行的一個參數和25行的一個getModel()方法。
注意20行中的mode參數要用new去獲取一個POJO類對象,如果沒有用new聲明,那么Struts將無法將前台數據傳遞過來。
此時要增加一個POJO類:Message.java
1 package com.basestruts.pojo; 2 3 import java.io.Serializable; 4 5 public class Message implements Serializable{ 6 7 /** 8 * 版本號 9 */ 10 private static final long serialVersionUID = 6023958776122034827L; 11 12 /** 名字 **/ 13 private String name; 14 15 /** 消息內容 **/ 16 private String message; 17 18 /* 省略name, message 的SET/GET方法 */ 19 }