the5fire

关注Python、Django、Vim、Linux、Web开发、团队管理和互联网--Life is short, we need Python.


Struts2web端和服务器端数据交互

作者:the5fire | 标签:   | 发布:2011-01-14 3:32 p.m. | 阅读量: 8976, 8752

上篇文章写了jsp页面或者html页面怎么通过url调用服务器端Action中的方法,那么web端和服务器端的数据应该怎么传递呢? 不知道大家是否记得在Servlet中是如何实现前段和后端的交互的,那里面是通过往request中设置参数,然后转发到页面上。在页面通过JSTL或者jsp代码接受,显示。 在struts2里面其实更加简单,只需要在Action中设置好一个属性的getter和setter方法即可。 这里做两个实验,通过两种不同的方式传递数据。不过有一点需要清楚,无论struts2是怎么做到不需要开发者操作request,但其本质依然是对request或者httprequest的操作。

实验一:传递值属性

一、Test1Action:

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class Test1Action extends ActionSupport {
    private String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String execute() throws Exception {
        return super.execute();
    }
}

二、 struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

    <constant name="struts.devMode" value="true"></constant>

    <package name="front" namespace="/" extends="struts-default">
        <action name="test*" class="com.action.Test{1}Action">
            <result>/test{1}.jsp</result>
        </action>
    </package>
</struts>

三、 jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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 'test.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>
   得到的数据是:<%=request.getAttribute("username") %>
  </body>
</html>

我直接在url中输入:

http://localhost:8088/struts2_test/test1?username=huyang

得到结果如下:

http://img208.poco.cn/mypoco/myphoto/20110114/15/5591354820110114152257084.jpg

<hr/>

实验二,传递对象 一、 Test2Action.java:

package com.action;

import com.domain.User;
import com.opensymphony.xwork2.ActionSupport;

public class Test2Action extends ActionSupport {
    private User user;

    @Override
    public String execute() throws Exception {
        return super.execute();
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

User.java:

package com.domain;

public class User {
    private String username;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}

配置文件不用改,test2.jsp页面:

<body>
得到的数据是:<%=request.getAttribute("User.username") %>
</body>

在浏览器输入url:

http://localhost:8088/struts2_test/test2?user.username=huyang

效果同上

那么如果我在User类中引用了另外一个类作为属性怎么传值呢?根据上例可知应该是User.[Class].username

其实在页面上可以使用Struts标签轻松的获取服务器发过来的数据,这样就可以完全不用考虑request了,但是其原理你要清楚的。

- from the5fire.com
----EOF-----

微信公众号:Python程序员杂谈


其他分类: