在JSP文件中调用java方法

时间:2012-06-06 16:47:14

标签: java jsp

我想在我的jsp页面调用我的方法getSelectedLayouts,方法是

public Iterable<Layouts> getSelectedLayouts(String Subject){
     Session sess=getCurrentSession();
     return sess.createCriteria(Layouts.class, Subject).list();
 }

内部类LayoutManager。我使用Spring Bean将LayoutManager传递到我的jsp页面

<custom:useSpringBean var="layoutManager" bean="LayoutManager">

jsp页面要求提供主题

<form method="post">
      <label for="subjectName">SubjectName:</label>
      <input type="text" name="subjectName" size="50" id="subjectName">
      <input class="button" type="submit" value="Search Layout" name="submit">
</form>

然后我转到

<jsp:useBean id="subjectName" class="LayoutManager">
    <c:if test="${param.submit!=null}">

           (here's where I want to call my getSelectedLayouts method)

    </c:if>

我一直在尝试使用scriplets,包括

的变体
<jsp:setProperty name="layout" property="*"/>
((LayoutManager)pageContext.getAttribute("layoutManager")).getSelectedLayout((La‌​youts)pageContext.getAttribute("layout")); 

或只是

<jsp:setProperty name="layout" property="*"/>
list<Layouts> = LayoutManager.getSelectedLayouts(layout); 

Layouts是我的对象类

请告诉我是否需要提供任何其他信息

编辑:当我尝试

LayoutManager layoutManager = new LayoutManager();
String subjectNa = request.getParameter("subjectName");
Iterable<Layouts> bla = layoutManager.getSelectedLayouts(subjectNa);

我收到错误列表

org.apache.jasper.JasperException: An exception occurred processing JSP page /search.jsp at line 72

Iterable<Layouts> waters = layoutManager.getSelectedLayouts(subjectNa);

java.lang.NullPointerException
com.amazon.basalt.examples.octane.tomcat.LayoutManager.getCurrentSession(LayoutManager.java:37)
com.amazon.basalt.examples.octane.tomcat.LayoutManager.getSelectedLayouts(LayoutManager.java:50)
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

    root cause

    java.lang.NullPointerException   com.amazon.basalt.examples.octane.tomcat.LayoutManager.getCurrentSession(LayoutManager.java:37)
com.amazon.basalt.examples.octane.tomcat.LayoutManager.getAllLayouts(LayoutManager.java:68)
org.apache.jsp.search_jsp._jspService(search_jsp.java:221)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

2 个答案:

答案 0 :(得分:0)

一种可能的解决方案。 (如果使用scriplets不是问题。)

<% 
LayoutManager layoutManager = new LayoutManager();
String subjectName = request.getParameter("subjectName");

layoutManager.getSelectedLayouts(subjectName);
%>

请记住在JSP中导入该类

答案 1 :(得分:0)

我建议你使用ajax来获取布局列表。