使用JSP脚本生成的索引访问Arraylist

时间:2019-04-25 20:40:30

标签: javascript java jsp servlets arraylist

我在servlet中有一个自定义对象的数组列表。


import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class Main extends JFrame {

    private JPanel contentPane;

    int xx, xy;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setUndecorated(true); // Hides the jframe top bar
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Main() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 735, 506);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(102, 102, 102));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel Forside = new JPanel();
        JPanel Dagbog = new JPanel();
        JPanel Aftaler = new JPanel();
        JPanel Kontakt = new JPanel();

        JPanel panelLeft = new JPanel();
        panelLeft.setBackground(new Color(51, 51, 51));
        panelLeft.setForeground(Color.DARK_GRAY);
        panelLeft.setBounds(0, 54, 150, 459);
        contentPane.add(panelLeft);
        panelLeft.setLayout(null);

        JButton btnForside = new JButton("Forside");
        btnForside.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                setCardLayoutView("name_1472174211097300");
            }
        });
        btnForside.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnForside.setForeground(Color.WHITE);
        btnForside.setFont(new Font("Tahoma", Font.PLAIN, 17));
        btnForside.setIcon(new ImageIcon(Main.class.getResource("/Images/icons8_Home_32px_1.png")));
        btnForside.setContentAreaFilled(false);
        btnForside.setBorderPainted(false);
        btnForside.setBorder(null);
        btnForside.setBounds(16, 60, 112, 30);
        panelLeft.add(btnForside);

        JButton btnDagbog = new JButton("Dagbog");
        btnDagbog.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setCardLayoutView("name_1472176236196000");
            }
        });
        btnDagbog.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnDagbog.setContentAreaFilled(false);
        btnDagbog.setBorderPainted(false);
        btnDagbog.setBorder(null);
        btnDagbog.setFont(new Font("Tahoma", Font.PLAIN, 17));
        btnDagbog.setForeground(Color.WHITE);
        btnDagbog.setIcon(new ImageIcon(Main.class.getResource("/Images/icons8_Book_32px.png")));
        btnDagbog.setBounds(16, 116, 112, 30);
        panelLeft.add(btnDagbog);

        JButton btnAftaler = new JButton("Aftaler");
        btnAftaler.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setCardLayoutView("name_1472177885026100");
            }
        });
        btnAftaler.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnAftaler.setContentAreaFilled(false);
        btnAftaler.setBorderPainted(false);
        btnAftaler.setBorder(null);
        btnAftaler.setForeground(Color.WHITE);
        btnAftaler.setFont(new Font("Tahoma", Font.PLAIN, 17));
        btnAftaler.setIcon(new ImageIcon(Main.class.getResource("/Images/icons8_Planner_32px.png")));
        btnAftaler.setBounds(16, 173, 112, 30);
        panelLeft.add(btnAftaler);

        JButton btnKontakt = new JButton("Kontakt");
        btnKontakt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setCardLayoutView("name_1472179607862700");
            }
        });
        btnKontakt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnKontakt.setContentAreaFilled(false);
        btnKontakt.setBorder(null);
        btnKontakt.setBorderPainted(false);
        btnKontakt.setFont(new Font("Tahoma", Font.PLAIN, 17));
        btnKontakt.setForeground(Color.WHITE);
        btnKontakt.setIcon(new ImageIcon(Main.class.getResource("/Images/icons8_Phone_32px.png")));
        btnKontakt.setBounds(16, 231, 112, 30);
        panelLeft.add(btnKontakt);

        JPanel panelTop = new JPanel();
        panelTop.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent arg0) {
                int x = arg0.getXOnScreen(); // makes uggerhøj picture dragable
                int y = arg0.getYOnScreen(); // makes uggerhøj picture dragable
                Main.this.setLocation(x - xx, y - xy);  // makes uggerhøj picture dragable
            }
        });
        panelTop.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                xx = e.getX(); // makes uggerhøj picture dragable
                xy = e.getY(); // makes uggerhøj picture dragable
            }
        });
        panelTop.setBackground(new Color(51, 51, 51));
        panelTop.setBounds(0, 0, 737, 60);
        contentPane.add(panelTop);
        panelTop.setLayout(null);

        JButton btnX = new JButton("X");
        btnX.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnX.setRolloverIcon(null);
        btnX.setFont(new Font("Tahoma", Font.BOLD, 18));
        btnX.setFocusTraversalKeysEnabled(false);
        btnX.setFocusPainted(false);
        btnX.setBorderPainted(false);
        btnX.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        btnX.setContentAreaFilled(false);
        btnX.setForeground(SystemColor.activeCaption);
        btnX.setBorder(null);
        btnX.setBounds(615, 13, 97, 25);
        panelTop.add(btnX);

        parentPanel = new JPanel();
        parentPanel.setBackground(Color.GRAY);
        parentPanel.setBounds(148, 54, 569, 405);
        contentPane.add(parentPanel);
        cardLayoutObject = new CardLayout(0, 0);
        parentPanel.setLayout(cardLayoutObject);

        parentPanel.add(Forside, "name_1472174211097300");
        Forside.setFocusable(false);

        JButton btnTest = new JButton("test");
        Forside.add(btnTest);

        parentPanel.add(Dagbog, "name_1472176236196000");

        JLabel lblTest = new JLabel("dagbog");
        Dagbog.add(lblTest);

        parentPanel.add(Aftaler, "name_1472177885026100");

        parentPanel.add(Kontakt, "name_1472179607862700");
    }
    private CardLayout cardLayoutObject;
    private JPanel parentPanel;

    private void setCardLayoutView(String viewName) {
        cardLayoutObject.show(parentPanel, viewName);
    }

}

在我的JSP中,我在Arraylist<Book> allBooks = new Arraylist<>(); 下拉列表中填充了<select>

allBooks

<select onchange="js_function()"> <option></option>.... </select> 在页面脚本中,在该脚本中,我可以获取有关用户选择的项目的所有信息,例如下拉列表中的项目序列号,如下所示:

js_function()

现在,如果我想通过该脚本中的var valueIndex = document.getElementById("my_books").selectedIndex; 访问allBooks,该如何实现?

谢谢。

0 个答案:

没有答案