qq_34424362 2019-04-23 18:55 采纳率: 50%
浏览 412
已结题

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

**
我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条回答 默认 最新

  • 殇洛 2019-04-23 20:34
    关注

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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题