struts2中如何利用链接来传递对象到action中


通过一个简单的示例来说明如何在JSP页面中通过链接的点击将对象传入到action中,并打印出传递成功后的结果~

材料准备:

struts2.3.41

下载地址:http://download.csdn.net/detail/u011351656/9591174

第一步:将struts2.3.41文件夹里的jar包复制到lib目录下,设计前端页面

<%@ 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> 
	<!-- 第一种传递方式 -->
    <a href="${pageContext.request.contextPath }/object_transmit.action?person.height=175&person.weight=65">点击传递person对象信息</a>  
    <!-- 第二种传递方式 --><br />
    <a href="object_transmit.action?person.height=175&person.weight=65">点击传递person对象信息</a>  
</body>
</html>

第二步:修改web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
id="WebApp_ID" version="3.1">
  <display-name>e_shopping</display-name>
  
  <!-- 首页 -->
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 指定Filter的名字,不能为空 -->
  <filter>  
    <filter-name>struts2</filter-name> 
    <!-- 指定Filter的实现类,此处使用的是Struts2提供的过滤类 --> 
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  </filter>  
    
	<!-- 定义Filter所拦截的URL地址 -->
  <filter-mapping>  
  	<!-- Filter的名字,该名字必须是filter元素中已声明过的过滤器名字 -->
    <filter-name>struts2</filter-name>  
    <!-- 定义Filter负责拦截的URL地址,如下表示拦截所有的请求,所有.action后缀的都会首先经过这个过滤器,这也是struts2的入口处。 -->
    <url-pattern>/*</url-pattern>  
  </filter-mapping>
  
</web-app>

第三步:设计实体对象

package com.cyl.model;

public class Person {
	private float height;//身高
	private float weight;//体重
	@Override
	public String toString() {
		return "Person [height=" + height + ", weight=" + weight + "]";
	}
	public Person(){
		
	}
	public Person(float height, float weight) {
		super();
		this.height = height;
		this.weight = weight;
	}
	public float getHeight() {
		return height;
	}
	public void setHeight(float height) {
		this.height = height;
	}
	public float getWeight() {
		return weight;
	}
	public void setWeight(float weight) {
		this.weight = weight;
	}
	
	
}

第四步:设计action用以接收请求和发送响应

注意:一定要实现Person对象的setter和getter方法才能接收前端页面发送过来的对象

package com.cyl.person.action;

import com.cyl.model.Person;
import com.opensymphony.xwork2.ActionSupport;

public class PersonAction extends ActionSupport {
	private Person person;
	

	public String transmit() {  
        System.out.println("----对象传递成功,开始打印对象信息----");  
        System.out.println(person);
        return "result";  
    }  
      
	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}  
    
}
第五步:配置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>  
    <package name="person" extends="struts-default">  
        <!-- category_update.actio: 访问update方法 -->  
        <action name="object_*" class="com.cyl.person.action.PersonAction" method="{1}">  
            <result name="result">/index.jsp</result>  
        </action>  
    </package>  
  
</struts>  

效果:

将工程项目发布到tomcat上,首页显示如下页面:


点击任何一个链接,在控制台中输出如下打印结果,这个时候就已经完成了将前端页面到服务器端对象的传递

工程项目下载地址:http://download.csdn.net/detail/u011351656/9591212

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值