使用struts2框架中3種客戶端向服務端發送請求參數的方式

1.action屬性:

前端參數直接在action屬性名之後,如PersonAction?id=1;

在對應的action中加入屬性id,必須加對應屬性的get(),set()方法,其中屬性名應爲小寫。

應用場景:客戶端向action中傳遞的參數不是一個完整的對象,而是簡單的n個參數;

2.領域對象(model):

前端參數名Action中bean類對象.屬性名,如有Person類的一個對象p,在Action
需要獲取,則前臺可表示爲p.name,p.age,其中name,age是person的屬性。

後臺Action中定義model類如Person的對象,必須加該對象的get(),set()方法;

應用場景:添加操作等發送的是一個對象的屬性或者多個對象的屬性或屬性太多;

3.模型接口方式:

前端直接是name屬性,不用再對象名.屬性名(p.name);

Action類實現ModelDriven<T>,重寫getModel()方法,定義一個bean類的對象並初始化(private  Person p=new  Person();或是在getModel(this.p = new Person());另種初始化的方式都可選);

應用場景:Action中接收的屬性只是1個bean對象的屬性,如表單添加1個bean類;

實現代碼如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <form action="personAction.action" method="post">
   	name:<input type="text" name="p.name"><br>
   	age:<input type="text" name="p.age"><br>
   	sex:<input type="text" name="p.sex"><br>
   	<input type="submit" value="ok">
   </form> 
   <a href="personAction?id=1">personaction</a>
   <hr>
   <form action="personAction" method="post">
   	name:<input type="text" name="name"><br>
   	age:<input type="text" name="age"><br>
   	sex:<input type="text" name="sex"><br>
   	<input type="submit" value="ok">
   </form>
  </body>
</html>
Ation中的代碼:

package com.handler;

import com.bean.Person;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class PersonAction implements ModelDriven<Person>{
	private Person p1 = new Person();
	private Person p;
	private int id;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Person getP() {
		return p;
	}
	public void setP(Person p) {
		this.p = p;
	}
	
	public String addPerson(){
		System.out.println(p.getName()+","+p.getAge());
		System.out.println(id+1);
		System.out.println(p1.getName());
		return "success";
	}
	public Person getModel() {
		//this.p1 = new Person();
		return p1;
	}
	
}
Struts.xml配置文件

<?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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

	<!-- package:劃分action
		1-name:包的名字  :本工程中唯一
		2-namespace:指定本包的命名空間  默認值:"" :任意路徑    
		3-  extends:繼承 
		  1-struts-default:struts的默認包
	 -->
	<!-- url:namespace+actionName -->
    <package name="default" namespace="/" extends="struts-default">
    	<action name="index">
    		<result>/index.jsp</result>
    	</action>
    	
    	<!-- 
    		action:映射action類
    		  1-name:唯一的id(url訪問時需要的名字):一個包裏要唯一
    		  2-class:指定action類  可選      默認:ActionSuport
    		  3-method:指定業務方法  可選       默認:execute
    	    result:映射返回結果   相應
    	      1-name:指定業務方法的返回值     默認:success
    	      2-url指定:/開頭
    	      3=type:指定跳轉方式    默認:請求轉發
    	 -->
    	<action name="personAction" class="com.handler.PersonAction" method="addPerson">
    		<result>/show.jsp</result>
    	</action>
    	
    </package>
</struts>



發佈了46 篇原創文章 · 獲贊 10 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章