struts2 的action 怎么向页面传值?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2 的action 怎么向页面传值?相关的知识,希望对你有一定的参考价值。

我是菜鸟,希望能详细点.
提前谢谢大家了!!
不是action 获得 页面的对象.
而是页面 获得 action 中的对象啊..

action向jsp传值的方法有三种:
1.用request.setAttribute()方法,不过只适用于请求转发,不可用于重定向
2.用session.setAttribute()方法
3.用struts2标签,在Java类中定义这个值对象,然后赋值,并且给出setter和getter方法,在jsp页面中,使用
<s:property value=""/>来获取
参考技术A 在Action类中获得HttpServletResponse对象的四种方法

在struts1.x Action类的execute方法中,有四个参数,其中两个就是response和request。而在Struts2中,并没有任何参数,因此,就不能简单地从execute方法获得HttpServletResponse或HttpServletRequest对象了。

但在Struts2 Action类中仍然有很多方法可以获得这些对象。下面就列出四种获得这些对象的方法。

【方法1】使用Struts2 Aware拦截器

这种方法需要Action类实现相应的拦截器接口。如我们要获得HttpServletResponse对象,需要实现org.apache.struts2.interceptor.ServletResponseAware接口,代码如下:

package action;

import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.*;
import org.apache.struts2.interceptor.*;

public class MyAction extends ActionSupport implements ServletResponseAware

private javax.servlet.http.HttpServletResponse response;
// 获得HttpServletResponse对象
public void setServletResponse(HttpServletResponse response)

this.response = response;

public String execute() throws Exception

response.getWriter().write("实现ServletResponseAware接口");



在上面的代码中,MyAction实现了一个ServletResponseAware接口,并且实现了setServletResponse方法。如果一个动作类实现了ServletResponseAware接口,Struts2在调用execute方法之前,就会先调用setServletResponse方法,并将response参数传入这个方法。如果想获得HttpServletRequest、HttpSession和Cookie等对象,动作类可以分别实现ServletRequestAware、SessionAware和CookiesAware等接口。这些接口都在org.apache.struts2.interceptor包中。

如果要获得请求参数,动作类可以实现org.apache.struts2.interceptor. ParameterAware接口,但如果只想判断某个参数是否存在,也可以实现com.opensymphony.xwork2.interceptor. ParameterNameAware接口。这个接口有一个acceptableParameterName方法,当Struts2获得一个请求参数时,就会调用一次。读者可以在这个方法中将所有的请求参数记录下来,以便以后使用。这个方法的定义如下:

boolean acceptableParameterName(String parameterName);

【方法2】使用RequestAware拦截器

这种方法和第1种方法类似。动作类需要实现一个org.apache.struts2.interceptor.RequestAware接口。所不同的是RequestAware将获得一个com.opensymphony.xwork2.util.OgnlValueStack对象,这个对象可以获得response、request及其他的一些信息。代码如下所示:

package action;

import java.util.Map;
import org.apache.struts2.*;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.*;
import com.opensymphony.xwork2.util.*;
import org.apache.struts2.interceptor.*;

public class FirstAction extends ActionSupport implements RequestAware

private Map request;
private HttpServletResponse response;

public void setRequest(Map request)

this.request = request;

public String execute() throws Exception

java.util.Set<String> keys = request.keySet();
// 枚举所有的key值。实际上只有一个key:struts.valueStack
for(String key: keys)
System.out.println(key);
// 获得OgnlValueStack 对象
OgnlValueStack stack = (OgnlValueStack)request.get("struts.valueStack");
// 获得HttpServletResponse对象
response = (HttpServletResponse)stack.getContext().get(StrutsStatics.HTTP_RESPONSE);
response.getWriter().write("实现RequestAware 接口");



我们也可以使用StrutsStatics.HTTP_REQUEST、StrutsStatics.PAGE_CONTEXT来获得HttpServletRequest和PageContext对象。这种方法有些麻烦,一般很少用,读者可以作为一个参考。

【方法3】使用ActionContext类

这种方法比较简单,我们可以通过org.apache.struts2.ActionContext类的get方法获得相应的对象。代码如下:

HttpServletResponse response = (HttpServletResponse)

ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);

HttpServletRequest request = (HttpServletRequest)

ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);

【方法4】使用ServletActionContext类

Struts2为我们提供了一种最简单的方法获得HttpServletResponse及其他对象。这就是org.apache.struts2.ServletActionContext类。我们可以直接使用ServletActionContext类的getRequest、getResponse方法来获得HttpServletRequest、HttpServletResponse对象。代码如下:

HttpServletResponse response = ServletActionContext.getResponse()

response.getWriter().write("hello world");

从这四种方法来看,最后一种是最简单的,读者可以根据自己的需要和要求来选择使用哪一种方法来获得这些对象。
参考技术B 这很容易。
首先我们来写一个Action类:
public class LoginAction
public String execute()
return SUCCESS;

public void setValue(SomeBean value)
this.value=value;

public SomeBean getValue()
return this.value;

private SomeBean value;

再写出Bean类:
public class SomeBean
public String getName()
public void setName(String name)

配置Action应该不用说了,没什么特别的,不会就上网查吧。
我们现在说方法。
方法一:
使用OGNL表达式。你可以使用struts自带的标签,他们都支持OGNL,比如s:property。举个例子:
<!--test.jsp-->
<%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:property value="value.name"/>

你访问LoginAction的时候s:property标签就会显示getValue().getName();
如果你想了解更多的struts2标签和OGNL表达式建议去找更详尽的资料。
方法二:
当然是使用JSP本身的性质了。通过request和session来获取值。
我们把Action类改一下:
public class LoginAction
public string execute()
SomeBean value=new SomeBean();
value.setName("sfsfjsfje");
ActionContext context=ActionContext.getContext();
//往request里放attribute
context.put("value",value);
//往session里放
context.getSession().put("value",value);
return SUCCESS;


接下来我们改页面:
<!-- test.jsp -->
<%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%= ((SomeBean) request.getAttribute("value")).getName() %>
<%= ((SomeBean) session.get("value")).getName() %>
就这些,没什么难度。本回答被提问者采纳
参考技术C 如果你不用struts2,你会怎么样传值呢?
方法相似:获得request对象
HttpServletRequest request= ServletActionContext.getRequest();
request.setAttribute("list");
页面get就可以了。

struts2中jsp页面与action之间的传值

一、从jsp页面中取值

a)      设计JavaBean

Java类

public class Student {
    String name;
    String sex;
    Long id;
    int age;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
  
}

b) 编写action类,定义变量且提供getter和setter方法,传递过来的参数会封装在stu中,用getter方法取值就可以

StudentAction类

public class StudentAction {
    //获取jsp中的学生信息
    Student stu ;
    public void setStu(Student stu) {this.stu = stu;}
    public Student getStu() {return stu;}
    
    public String execute() throws Exception {
        System.out.println("已提交学生是:"+stu1.name+" "+stu1.age+"岁  "+stu1.sex+" "+stu1.id+" "
                +address1.province+address1.city);
                return "loginresult";
    };
}

c)      设计表单,在jsp页面中直接用  <对象名.属性名>获取.(emmm。。其中过程我也有点懵,只知道可以这样写,怕忘了所以先记着)

student.jsp

<form action="StudentAction">
           学生姓名:<input type="text" name="stu.name"/></br></br>
           学生年龄:<input type="text" name="stu.age"/></br></br>
           学生编号:<input type="text" name="stu.id"/></br></br>
           学生性别:<input type="text" name="stu.sex"/></br></br>
           学生籍贯:<input  type="text" name="address.province"/></br></br>
           学生地址 :<input  type="text" name="address.city"/></br></br>
           
           <input type="submit" value="提交">
  </form>

 

d) 配置好web.xml和struts.xml文件

struts.xml

<struts>
    <constant name="struts.devMode" value="true"/>
    <package name="gsonTest" extends="struts-default">
 
        <action name="StudentAction" class="gsonTest.StudentAction">
         <result name="loginresult">success.jsp</result>
        <result name="input">/error.jsp</result>
        
        </action>
        
    </package>
    
</struts>

package:name=“包名”

action : name = "jsp页面中的action名称" ,class = "包名.action类名"

 

d)  输出结果

技术图片

 

二、action传值到jsp页面

1)      在action类中一定义一个成员变量,然后对这个成员变量提供get/set方法

StudentAction类

    Student stu ;
    public void setStu(Student stu) {this.stu = stu;}
    public Student getStu() {return stu;}        

2)    导入struts标签库,用<s>标签获取值,或者用el表达式 ${stu.getName()}

success.jsp

${stu.getName()};
 学生姓名:<s:property value="stu.getName()"/></br></br>
 学生编号:<s:property value="stu.getId()"/></br></br>
 学生性别:<s:property value="stu.getSex()"/></br></br>
 学生年龄:<s:property value="stu.getAge()"/>

3) 效果

技术图片

技术图片

 

以上是关于struts2 的action 怎么向页面传值?的主要内容,如果未能解决你的问题,请参考以下文章

如何从jsp页面向后台传值?

struts2中jsp页面与action之间的传值

struts2中的sessionrequest 和action往页面中传值的方法

怎么把action中的值传到jsp页面

关于STRUTS2的传值问题?

struts2怎么向jsp传递参数