如何使用spring mvc将值jsp页面传递给java?

时间:2017-11-17 11:47:56

标签: java spring jsp spring-mvc spring-boot

我正在使用带有hibernate数据库连接的spring mvc,现在如何在没有调用控制器的情况下传递jsp page into java page中的值,我得到错误如何将值jsp传递给java ??

Jsp页面

在此页面中,我使用的是java代码,但无法在另一个java页面中传递值

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@page import="com.sample.utility.AutoIncrement" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>check page</title>
</head>
<body>

       <div id="login_form">
   <form:form action="check" method="post"> 
        <table>

            <tr>             
             <%String val=AutoIncrement.auto("ShopInfo"); System.out.println(" val : "+val);%>                                                
                <td class="f1_label">Shop code :</td>                
                <td>                
                <input type="text" name="shopcode" value="<%=val%>" />               

                </td>
            </tr>
            <tr>
                <td class="f1_label">name  :</td><td><input type="text" name="name" value=""  />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" name="login" value="Submit" style="font-size:18px; " />
                </td>
            </tr>
        </table>
    </form:form> 
</div>  

</body>
</html>

Java页面:

以下代码是java这里无法返回jsp页面中的值

 public static String auto(String value)
        {
            String reVal="";
            System.out.println("Inc val : "+value);
            Session session = sessionFactory.openSession();
            try     
            {                                   
                session.beginTransaction().begin();
                String SQL_QUERY = "{CALL pro_autoincrement('"+value+"') }";
                query = session.createSQLQuery(SQL_QUERY);  
                reVal=query.toString();
                System.out.println("Inc val : "+reVal);
                session.getTransaction().commit();
           }
            catch(Exception ex)
            {       
            if ( session.getTransaction().getStatus() == TransactionStatus.ACTIVE
                    || session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK ) {
                session.getTransaction().rollback();
            }
            }
            finally
            {           
                session.close();
            }
            return reVal;       
        }

错误:

SEVERE: Servlet.service() for servlet [jsp] in context with path [/annotation-based] threw exception [An exception occurred processing JSP page /welcome.jsp at line 18

15:         <table>
16:         
17:             <tr>             
18:              <%String val=AutoIncrement.auto("ShopInfo"); System.out.println(" val : "+val);%>                                                
19:                 <td class="f1_label">Shop code :</td>                
20:                 <td>                
21:                 <input type="text" name="shopcode" value="<%=val%>" />               


Stacktrace:] with root cause
java.lang.NullPointerException
    at com.sample.utility.AutoIncrement.auto(AutoIncrement.java:26)
    at org.apache.jsp.welcome_jsp._jspService(welcome_jsp.java:81)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)

1 个答案:

答案 0 :(得分:0)

考虑到这一点。 REVAL = query.toString();

你可以这样做

REVAL =(字符串)session.createQuery(的queryString).UniqueResult();

上面的方法返回Object.So我们需要输入Cast into String Format。