将变量传递给JSP Java中的方法(Pass variable to a method in JSP Java)

编程入门 行业动态 更新时间:2024-10-23 15:21:43
变量传递给JSP Java中的方法(Pass variable to a method in JSP Java)

我有一个Vector方法,它将数据库中的值返回给JSP组合框。 该方法有一个输入变量字符串,存储在会话中。 我需要将Session中的值传递给JSP中的方法调用,但我没有从数据库中获取值。 这是我的方法:

public Vector getHostel(String campus) throws IOException{ Vector v = new Vector(); Connection conn; try{ conn = db.getDbConnection(); String sql = "select * from hostel_master where campus_code = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, campus); ResultSet rs = ps.executeQuery(); while(rs.next()){ String host = rs.getString(1); v.add(host) } }catch(Exception asd){ System.out.println(asd.getMessage()); } return v; }

我的JSP文件提取:

<jsp:useBean id = "obj" class="com.app.dao.HostelsDao" scope = "page"/> <% String campus = (String)session.getAttribute("campus_code");%> <%-- I really hate this --%> <option selected value = "SELECT">SELECT</option> <c:forEach var = "item" items = "${obj.getHostel(campus)}"> <option>${item}</option> </c:forEach>

我没有在我的选择列表中获取任何内容但是当我直接传递值时,我得到了正确的输出。 我需要根据会话中的campus_code值来改变此值。 请帮忙。

I have a Vector Method which returns value from the database to a JSP combobox. The Method has an Input Variable String which is stored in the session. I need to Pass the Value from the Session to the method invocation in the JSP but I am not getting the values from the Database. Here is My Method:

public Vector getHostel(String campus) throws IOException{ Vector v = new Vector(); Connection conn; try{ conn = db.getDbConnection(); String sql = "select * from hostel_master where campus_code = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, campus); ResultSet rs = ps.executeQuery(); while(rs.next()){ String host = rs.getString(1); v.add(host) } }catch(Exception asd){ System.out.println(asd.getMessage()); } return v; }

And My JSP file extract:

<jsp:useBean id = "obj" class="com.app.dao.HostelsDao" scope = "page"/> <% String campus = (String)session.getAttribute("campus_code");%> <%-- I really hate this --%> <option selected value = "SELECT">SELECT</option> <c:forEach var = "item" items = "${obj.getHostel(campus)}"> <option>${item}</option> </c:forEach>

I am not getting Anything in my select list but when I pass the Value directly I am getting correct output. I need this value to vary depending on the campus_code value in the session. Please Help.

最满意答案

而不是EL调用obj.getHostel(campus) ,在您的DAO中,您可以拥有campus_code的类属性和setter。

然后,您可以在useBean标记之后使用<c:set target="${obj}" property="campusCode">设置值。 然后,您只需将DAO方法getHostel(校园)更改为一个简单的getter getHostels() ,可以使用${obj.hostels}进行访问。

可以使用隐式会话对象在JSTL中访问会话: ${session}因此您应该能够执行${session.campus_code}或${session["campus_code"]}而不是使用scriptlet。

Rather than the EL call to obj.getHostel(campus), in your DAO you could have class property and setter for campus_code.

You could then set the value using a <c:set target="${obj}" property="campusCode"> after the useBean tag. You would then just change your DAO method getHostel(campus) to a simple getter getHostels() which can be accessed with ${obj.hostels}.

The session can be accessed in JSTL with the implicit session object: ${session} so you should be able to do ${session.campus_code} or ${session["campus_code"]} rather than using the scriptlet.

更多推荐

JSP,String,campus_code,电脑培训,计算机培训,IT培训"/> <meta name="descri

本文发布于:2023-07-24 20:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250566.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   方法   JSP   method   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!