使用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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值