如何从Struts2 action类传值到jsp页面中 40C

**
我set进JAVAben类的数据在jsp里面取不到值,有没有人能帮帮忙,好久没能解决这个问题。最好是能直接把代码列出来。**

设置数据代码

public class ShowShare extends ActionSupport{



    String title  ;
    public HashMap<String, String> info=new HashMap<String, String>();
    public List<String>list;
    public String getTitle() {
        return title;
    }
    public HashMap<String, String> getInfo() {
        return info;
    }
    public void setInfo(HashMap<String, String> info) {
        this.info = info;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getImg1() {
        return img1;
    }
    public void setImg1(String img1) {
        this.img1 = img1;
    }
    public String getImg2() {
        return img2;
    }
    public void setImg2(String img2) {
        this.img2 = img2;
    }
    public String getImg3() {
        return img3;
    }
    public void setImg3(String img3) {
        this.img3 = img3;
    }
    String content  ;
    String img1;
    String img2;
    String img3;

public String execute(){

        System.out.println("1"+title);
        System.out.println("1"+content);
        System.out.println("1"+img1);
        System.out.println("1"+img2);
        System.out.println("1"+img3);
        ShowShareJavaBean show1d=new ShowShareJavaBean();
        show1d.setTitle(this.getTitle());
        show1d.setContent(this.getContent());
        show1d.setImg1(this.getImg1());
        show1d.setImg2(this.getImg2());
        show1d.setImg3(this.getImg3());

        ActionContext.getContext().getSession().put("title",this.getTitle());
        ActionContext.getContext().getSession().put("content",this.getContent());
        ActionContext.getContext().getSession().put("img1",this.getImg1());
        ActionContext.getContext().getSession().put("img2",this.getImg2());
        ActionContext.getContext().getSession().put("img3",this.getImg3());
        System.out.println("4"+show1d.getTitle());
        System.out.println("4"+show1d.getContent());
        System.out.println("4"+show1d.getImg1());
        System.out.println("4"+show1d.getImg2());
        System.out.println("4"+show1d.getImg3());
        info.put("success", "success");
        return SUCCESS;
    }

}

JSP代码

<%@page import="com.yiibai.user.action.ShowShare"%>
<%@page import="com.yiibai.utils.ShowShareJavaBean"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page language="java" import="java.util.* ,java.awt.*" errorPage="jsps/error.jsp" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
        <meta content="width=device-width initial-scale=1.0 maximum-scale=1.0 user-scalable=yes" name="viewport" />

        <meta name="description" content="Responstent is a jQuery plugin that automagically makes your user generated content responsive. Video's, tables, images and maps, respontent'll make it fit." />
        <title>内容分享详情 </title>

        <script src="http://www.jq22.com/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript" src="js/layout.js"></script>
        <script type="text/javascript" src="js/jquery.respontent.min.js"></script>




        <link type="text/css" rel="stylesheet" href="css/normalize.css" />
        <link type="text/css" rel="stylesheet" href="css/style.css" />
        <link type="text/css" rel="stylesheet" href="css/layout.css" />
        <link type="text/css" rel="stylesheet" href="css/jquery.respontent.css" />

    </head>
    <body>
    <jsp:useBean id="student" class = "com.yiibai.utils.ShowShareJavaBean" scope="session" ></jsp:useBean>
    <%
    ShowShareJavaBean showshare1=new ShowShareJavaBean();
    String title=showshare1.getTitle();
    String content=showshare1.getContent();
    String img1=showshare1.getImg1();
    String img2=showshare1.getImg2();
    String img3=showshare1.getImg3();
    System.out.println("2"+title);
    System.out.println("2"+content);
    System.out.println("2"+img1);
    System.out.println("2"+img2);
    System.out.println("2"+img3);

%>


        <nav>
            <ul>

                <li><a href="#examples">Examples</a></li>

                <li><a href="#about">About</a></li>
            </ul>
        </nav>



        <div id="wrapper">
        <section id="about">


                <div class="explanation">

                    <h3><span> <jsp:getProperty name="student" property="title"/> </span></h3>
                    <p class="text"><%=content%></p>
                </div>
            </section>
          <section id="examples">
              <div class="example">
                    <h3><span>Images</span></h3>
                    <div>
                        <div class="w">

                          <img src=<%=img1%> width="500" height="290"/>
                        </div>

                        <div class="wo">

                          <img src=<%=img2%> width="500" height="290"/>
                        </div>
                        <div class="w" >

                          <img src=<%=img3%>  width="500" height="290"/>
                        </div>
                    </div>
            </div>





          </section>



    </div>
    </body>
</html>

JavaBean代码

public class ShowShareJavaBean implements Serializable{

    public String title  ;
    public String content  ;
    public String img1;
    public String img2;
    public ShowShareJavaBean(){

    }

public ShowShareJavaBean(String title,String content,String img1,String img2,String img3){
        super();
        this.title=title;
        this.content=content;
        this.img1=img1;
        this.img2=img2;
        this.img3=img3;
    }


    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getImg1() {
        return img1;
    }
    public void setImg1(String img1) {
        this.img1 = img1;
    }
    public String getImg2() {
        return img2;
    }
    public void setImg2(String img2) {
        this.img2 = img2;
    }
    public String getImg3() {
        return img3;
    }
    public void setImg3(String img3) {
        this.img3 = img3;
    }
    public String img3;
}

4个回答

ShowShareJavaBean showshare1=new ShowShareJavaBean();
这里,你的showshare1是new出来的,而你execute()在里面才设置了属性,你又没有调用它,自然没有值

qq_34424362
qq_34424362 能取到数据就行
10 个月之前 回复
qq_34424362
qq_34424362 那应该怎么去改呢.
10 个月之前 回复

session是非常好用的

通过值栈 ActionContext context = ActionContext.getContext();ValueStack stack = context.getValueStack()
调用值栈对象中的set方法
stack.set("demo", "DemoData"); 也可以是put, 也可以给对象提供get方法,直接就会返回到值栈

u012976158
强化脑细胞 在页面用s:debug看值栈有没有你存的值,如果有,就是你表达式的问题,<s:property value="name"/> 这个需要看你是存的对象,还是直接存的属性
10 个月之前 回复
qq_34424362
qq_34424362 用${}拿不到值 ,用<s:property value="name"/> 直接报错。
10 个月之前 回复

你可以吧整个后台设置的数据全部装在session中,这样的话你在前台直接就可以用session里面的key点出点出你所需要的数据,如
request.getSession.setAttribute("show1d",show1d);(说明:双引号里面的show1d是key,而逗号外面的是你当前的整个数据),
这样你在前台用的话可以:“ src="${show1d.img1}" width="500" height="290"/。

ps:只是个人建议,无用请勿喷,谢谢!!

qq_36942738
隔壁老陈你瞅啥 回复qq_34424362: 为什么会没有 ActionSupport好像是可以拿到request的 或者是在action上implements ServletRequestAware
10 个月之前 回复
weixin_43869661
殇洛 回复qq_34424362: ok,sorry
10 个月之前 回复
qq_34424362
qq_34424362 我这个不是servlet
10 个月之前 回复
qq_34424362
qq_34424362 类里都没有request 这个变量,调用不了
10 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问