ssh框架spring简单实例取值问题,急!!在线等!!

tiktok 发布于 2014/06/28 15:48
阅读 481
收藏 0

主要的项目实例是通过ssh框架去数据库取值,并返回在页面上

数据表book内容如下

BookID  BookName  Author Page Stock

1    jsp           shen          430    5
2    hibernate    wang    210    10
3    css    lin    200    23
4    spring    zhang    170    6

我的页面上,主要是实现输入书名(比如jsp)时,返回库存数(5)

当通过spring容器传值时,碰到问题,就是sql无法取回,我把代码贴在下面了:

springDepacher.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <!-- 内网统计  -->
    <bean id="reportDao_InnerNet" parent="transactionProxyBean_SqlServer" scope="singleton">
        <property name="target">
            <bean class="com.wang.persistence.BookDAOImpl">
                <property name="dataSource">
                    <ref bean="dataSource_SqlServer"/>
                </property>
                <!-- 查找图书库存SQL -->
                <property name="bookStocksql">
                    <value><![CDATA[
                    select * from test.dbo.book where BookName=?
                    ]]></value>
               </property>

           </bean>
           </property>
     </bean>
    
</beans>




BookDAO.java

package com.wang.persistence;

import com.wang.model.*;
import java.util.List;
import org.hibernate.*;



public interface BookDAO{

    //public boolean Save(Book book) throws Exception;

    public int getBookStock(String title);
}




BookDAOImpl.java

package com.wang.persistence;
import com.wang.model.*;
import java.util.List;
import org.hibernate.*;

import org.springframework.jdbc.core.support.JdbcDaoSupport;


public class BookDAOImpl extends JdbcDaoSupport implements BookDAO{
    
    private String bookStocksql;
    
    public void setBookStocksql(String bookStocksql) {
        this.bookStocksql = bookStocksql;
    }

    /**
     * @return the getBookStocksql
     */
    public String getBookStocksql() {
        return bookStocksql;
    }

    public int getBookStock(String title){
        try{
            int count = 0;
            String sql = getBookStocksql();
            //sql = sql.replaceAll("#\\{unit\\}",unitCode);
            
            System.out.println("bookStock SQL:" + sql);
    
            Object[] args = new Object[]{title};
            count = getJdbcTemplate().queryForInt(sql, args);
            return count;
        }catch (Exception e){
            System.out.println("getReplyCountByDay Exception:" + e.getMessage());
            return -1;
        }
        
    }
    
}



调用的struts没啥问题,因为在加入spring之前调试已经成功,下面是BookAction类

BookAction.java

package com.wang.struts;

import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.hibernate.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wang.model.Book;
import com.wang.persistence.*;


public final class BookAction extends Action
{

    public ActionForward execute(ActionMapping mapping,
            ActionForm form, HttpServletRequest req,
            HttpServletResponse res){
        int stk=0;

        Book bk = new Book();
        System.out.println("Start perform(" +form+")...");
        BookForm bookform = (BookForm)form;
        String title= bookform.getBookName();
        
        System.out.println("After creation of book:"+ title);

        Book bk2= new Book();
                int st =  new BookDAOImpl().getBookStock(title);
        
            bk2.setBookName(title);
            bk2.setStock(st);
   
 
        req.setAttribute("book", bk2);
        return mapping.findForward("bookCreated");
    }
    
}



BookView.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<html:form action="createBook" method="GET">
	Title:<html:text property="bookName" /> <br/>
	<html:submit property="submit"/>
</html:form>


CreateBook.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ page import = "com.wang.model.*" %>	
<%
	//BookForm bookForm = (BookForm)request.getAttribute("bookForm");
	Book book = (Book)request.getAttribute("book");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
	<html:base/>

</head>

<body bgcolor="white">
<h2>book name check: <%=book.getBookName()%> </h2>
<h2>Left in stock:<%=book.getStock()%></h2>

<html:errors/>

</body>
</html:html>

调试时,输入jsp,返回如下:

book name check: jsp

Left in stock:-1


console打印信息如下:

2014-6-28 15:17:38 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Genuitec\Common\binary\com.sun.java.jre.win32.x86_1.5.0.011\bin;D:\Program Files\Apache Software Foundation\Tomcat 6.0\bin
2014-6-28 15:17:38 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2014-6-28 15:17:38 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 764 ms
2014-6-28 15:17:39 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2014-6-28 15:17:39 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.39
log4j:WARN No appenders could be found for logger (org.apache.struts.util.PropertyMessageResources).
log4j:WARN Please initialize the log4j system properly.
2014-6-28 15:17:40 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor host-manager.xml
2014-6-28 15:17:41 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor manager.xml
2014-6-28 15:17:41 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory docs
2014-6-28 15:17:41 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory examples
2014-6-28 15:17:41 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
2014-6-28 15:17:41 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
2014-6-28 15:17:41 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory ROOT
2014-6-28 15:17:41 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory Struts2Example
2014-6-28 15:17:42 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory Struts2Example.myeclipse.bak
2014-6-28 15:17:42 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory strutsShop
2014-6-28 15:17:42 org.apache.catalina.core.StandardContext addApplicationListener
信息: The listener "org.springframework.web.context.ContextLoaderListener" is already configured for this context. The duplicate definition has been ignored.
2014-6-28 15:17:42 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
2014-6-28 15:17:44 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2014-6-28 15:17:44 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2014-6-28 15:17:44 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/31  config=null
2014-6-28 15:17:44 org.apache.catalina.startup.Catalina start
信息: Server startup in 5868 ms
2014-6-28 15:19:26 org.apache.catalina.core.ApplicationContext log
信息: HTMLManager: init: Associated with Deployer 'Catalina:type=Deployer,host=localhost'
2014-6-28 15:19:26 org.apache.catalina.core.ApplicationContext log
信息: HTMLManager: init: Global resources are available
2014-6-28 15:19:26 org.apache.catalina.core.ApplicationContext log
信息: HTMLManager: list: Listing contexts for virtual host 'localhost'
com.wang.model.book.getBookName()
com.wang.model.book.getBookName()
com.wang.model.book.getBookName()
Start perform(com.wang.struts.BookForm@115512)...
com.wang.model.book.getBookName()
After creation of book:jsp
bookStock SQL:null
getReplyCountByDay Exception:null
com.wang.model.book.getBookName()
com.wang.model.book.getStock()


请大神帮忙看看为啥sql取不到啊?急等!!!!!!!!

加载中
返回顶部
顶部