我想将值从控制器重定向到struts2中的jsp页面

时间:2017-02-08 14:09:32

标签: jsp struts2

这是我的democontroller.java

package com.viewsite.model.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.plotwell.Dao.MyConnection;

public class democontroller extends ActionSupport implements SessionAware {

    private String uact;


    public String getUact() {
        return uact;
    }

    public void setUact(String uact) {
        this.uact = uact;
    }


    @Override
    public void setSession(Map<String, Object> arg0) {


        // TODO Auto-generated method stub

    }

    public String execute() throws SQLException, IOException,ClassNotFoundException 
    {
        System.out.println("hello");
            //democontroller object = new democontroller();
            System.out.println("uact aa gaya"+getUact());
            String uaact = getUact();
            return "graphs/jsp/demo.jsp";

    }
}

这是我的demo.jsp页面

<%@page import="com.viewsite.model.controller.democontroller"%>
<%@page import="com.viewsite.model.controller.ClientDropdown"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="org.apache.struts2.dispatcher.SessionMap" %>
<%@ page import="org.apache.struts2.interceptor.SessionAware" %>
<%@ page import="com.plotwell.Dao.MyConnection"%>
<%@ page import="com.login.model.LoginAction" %>
<%@ page import="com.plotwell.model.SiteInfo" %>
<%@ page import="com.viewsite.model.controller.ClientDropdown" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<
<html>
<head>
<title>Demo</title>
</head>
<body>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
</body>
<%
    ClientDropdown cd = new ClientDropdown();
    democontroller obj = new democontroller();
    System.out.println("idhar bhi aa gaya" +obj.getUact());

    System.out.println("inside scriptlet");
    session.getAttribute("id");
    LoginAction loginAction = new LoginAction();
    String id = loginAction.getId();
    System.out.println("in client:::"+session.getAttribute("id"));
    MyConnection conn = new MyConnection();
    SiteInfo siteInfo = new SiteInfo();
    try{
        Connection con = conn.getconnection();
        PreparedStatement ps = con.prepareStatement("SELECT * FROM graph WHERE ucid = ? ");
        ps.setString(1, (String)session.getAttribute("id"));
        ResultSet rs = ps.executeQuery();
        //System.out.println("data "+rs.getString("uact"));
            /* System.out.println("data "+rs.getString("uact"));
            String uact=rs.getString("uact");
            PreparedStatement ps1 = con.prepareStatement("SELECT * FROM transaction WHERE uact = '"+uact+"' order by instno" );
            ResultSet rs1 = ps1.executeQuery();
            while(rs1.next()){
                //System.out.println("tid  "+rs1.getString("tid"));
            } */ 



%>
<script type="text/javascript">
Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'Total paid amount according to Installment NO'
    },
    subtitle: {
        text: 'Source: <a href="cluebix.com">Cluebix</a>'
    },
    xAxis: {
     title: {
            text: 'Install no'
        },
       categories: ['1', '2', '3', '4', '5', '6',
                    '7', '8', '9', '10', '11', '12','13', '14','15',
                    '16','17', '18','19', '20','21', '22','23', '24','25', '26','27', '28','29', '30'] 
   },
    yAxis: {
        min: 0,
        title: {
            text: 'paid amount'
        }
    },
    legend: {
        enabled: false
    },
    tooltip: {
        pointFormat: 'paid amount: <b>{point.y:.1f} </b>'
    },
    series: [{
        name: 'Site Name',
        data: [

               <%
               while(rs.next()) 
            { 
               System.out.println("tid  "+rs.getString("uact"));   
               %>

            ['<%=rs.getString("uact")%>', <%=rs.getString("paidamt")%>],

            <%
         }
            %>

        ],
        dataLabels: {
            enabled: true,
            rotation: -90,
            color: '#FFFFFF',
            align: 'right',
            format: '{point.y:.1f}', // one decimal
            y: 10, // 10 pixels down from the top
            style: {
                fontSize: '13px',
                fontFamily: 'Verdana, sans-serif'
            }
        }
    }]
});
</script>

<%
    ps.close();
con.close();
    }
    catch(Exception e)
    { 
        out.clear();
        out = pageContext.pushBody();
        out.println("Page Not Found");
    } 
%>

</html>

在上面的控制器中,我从ajax调用得到了uact的值,但现在我想在demo.jsp上使用这个值。

如何将此值从控制器重定向到jsp页面。

这将帮助我制作图表。

我多次尝试但失败了。

1 个答案:

答案 0 :(得分:1)

那段代码完全是胡说八道。

  1. 你不是
      

    从ajax调用中获取uact的值

  2. ,你是从getter获取它,它读取uact变量,然后你将它分配回同一个变量。 埃舍尔,有人吗?

    1. 您返回JSP的路径,但必须返回映射到struts.xml文件中的JSP的结果。这是Struts2的基础。

    2. 您在JSP页面中大量使用scriptlet(<% %>); scriptlet很糟糕。无论如何,再也不要使用sctiplets。永远,永远,永远。使用Struts2标记,JSTL标记或JSP EL等。不是scriptlet。

    3. 在修复业务逻辑,结果映射和JSP结构中的错误之后,您现在不会遇到任何问题。

      你可能还有其他问题/疑问,但到那时你应该能够回到这里问一个真的负责的问题。