qq_34424362
qq_34424362
2019-04-23 18:55

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

40
  • java
  • tomcat
  • struts

**
我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条回答

为你推荐

换一换