数据库值未显示在网页中? [英] Database values not displaying in the webpage?

查看:69
本文介绍了数据库值未显示在网页中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用Struts 2 + Hibernate.不过,我在这个领域还比较陌生.我能够在Java代码中获取所需的值,但无法在jsp页面中获取.

I am using Struts 2 + hibernate in my project. I am relatively new to this field though. I am able to get the required values in java code but not able to get in the jsp page.

这是我的xml代码:

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="default" extends="hibernate-default">

    <action name="addTweets" method="add" class="com.vaannila.web.TweetAction">
            <result name="success" type="redirect">listTweet</result>
        </action>
        <action name="listTweet" method="list" class="com.vaannila.web.TweetAction">
            <result name="success">/showTweet.jsp</result>
        </action>
    </package>
</struts>

这是我的jsp页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home Page.</title>

 <%@taglib uri="/struts-tags" prefix="s"%>

</head>
<body>

<h2>Welcome
<%
String username = request.getParameter("username");
out.println(username);
%>
</h2>

<div>
Tweet:
<s:form action="message">
<s:textarea name="message" />
<s:submit />
</s:form>
</div>

Show Tweets:
<s:form action="listTweet" >
<s:submit />
</s:form>

</body>
</html>

在jsp中,单击显示推文"按钮时,它应映射到struts.xml中的"listTweet",并应转到"TweetAction.java"类的"list"方法.

From jsp, on clicking show tweet button, it should map to the "listTweet" in struts.xml and should go to 'list' method of "TweetAction.java" class.

TweetAction.java:

TweetAction.java:

package com.vaannila.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.vaannila.dao.TweetDAO;
import com.vaannila.dao.TweetDAOImpl;
import com.vaannila.dao.UserDAO;
import com.vaannila.dao.UserDAOImpl;
import com.vaannila.domain.Tweet;
import com.vaannila.domain.User;

    public class TweetAction extends ActionSupport implements ModelDriven<Tweet>,SessionAware{

        private User user = new User();
        private List<User> userList = new ArrayList<User>();
        private UserDAO userDAO = new UserDAOImpl(); //UserDAO interface, UserDAOImpl implements it.
        private boolean isAuthentic = false;

        private Tweet tweet = new Tweet();
        private List<Tweet> tweetList = new ArrayList<Tweet>();
                        //TweetDAO interface, TweetDAOImpl implements it.
        private TweetDAO  tweetDAO = new TweetDAOImpl();

        public Tweet getModel() {
            // TODO Auto-generated method stub
            return tweet;
        }   


    public String list()
    {
        System.out.println("inside list method");
        tweetList = tweetDAO.listTweet();
        System.out.println("exiting list method");
        return SUCCESS;
    }


    public String add()
    {
        System.out.println("inside put message");
        tweet.setUser_id(user.getUser_id());
        System.out.println(user.getUser_id());
        tweetDAO.saveTweet(tweet);
        return SUCCESS;
    }

    public String showTweet()
    {
        System.out.println("inside list method");
        tweetList = tweetDAO.listTweet();
        System.out.println("exiting list method");
        return SUCCESS;
    }
}

此处为"TweetDAO.java"界面:

Here 'TweetDAO.java' interface:

package com.vaannila.dao;

import java.util.List;

import com.vaannila.domain.Tweet;

public interface TweetDAO {

    public void saveTweet(Tweet tweet);
    public List<Tweet> listTweet(); 

}

这是TweetDAOImpl:

Here's TweetDAOImpl:

package com.vaannila.dao;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
import com.vaannila.domain.Tweet;

public class TweetDAOImpl implements TweetDAO,SessionAware {

    @SessionTarget
    Session session;
    @TransactionTarget
    Transaction transaction;
    Map<String, Object> session1;

    public void saveTweet(Tweet tweet) {
        try {
            System.out.println("Update successful..");
            session.save(tweet);
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
            e.printStackTrace();
        } 
    }



    public void setSession(Map<String, Object> session) {
        session1 = session;

    }



    public List<Tweet> listTweet() {
        List<Tweet> courses = null;
        try
        {
            System.out.println("entered dao impl");
            SQLQuery query = session.createSQLQuery("select message,created from tweet");
            courses=query.list();
            System.out.println("dao impl "+courses);

        } catch (Exception e) 
            {
            System.out.println("sorry entered exception");
                e.printStackTrace();
            } 
                return courses;
    }
}

我能够在课程"中获得价值.使用System.out.println("dao impl "+courses);时,它会给出跟随输出:dao impl [[Ljava.lang.Object;@3d2178, [Ljava.lang.Object;@1607a8a, [Ljava.lang.Object;@10d04fc, [Ljava.lang.Object;@1c27660, [Ljava.lang.Object;@1e99fae]

I am able to get the value in 'courses'. On using System.out.println("dao impl "+courses);, it gives the followign output: dao impl [[Ljava.lang.Object;@3d2178, [Ljava.lang.Object;@1607a8a, [Ljava.lang.Object;@10d04fc, [Ljava.lang.Object;@1c27660, [Ljava.lang.Object;@1e99fae]

因此,课程中至少有一些内容.但是在.jsp页面中,它进入else循环. 错误在哪里?

So theres is atleast something in courses. But in .jsp page, it goes in the else loop. Where's the error?

这是我的堆栈跟踪:

Dec 01, 2014 4:36:09 PM org.apache.catalina.core.AprLifecycleListener init
INFO: 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 (x86)\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files (x86)/Java/jre7/bin/client;C:/Program Files (x86)/Java/jre7/bin;C:/Program Files (x86)/Java/jre7/lib/i386;D:\app\trg\product\11.2.0\client_1\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\CheckPoint\File Encryption\Program\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies;C:\Program Files (x86)\Java\jdk1.7.0_45\bin;C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin;D:\New folder\eclipse;;.
Dec 01, 2014 4:36:09 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:StrutsIntegHib' did not find a matching property.
Dec 01, 2014 4:36:09 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Dec 01, 2014 4:36:09 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 474 ms
Dec 01, 2014 4:36:09 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Dec 01, 2014 4:36:09 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/core_rt is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/core is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/core is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt_rt is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/fmt is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/functions is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/permittedTaglibs is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/scriptfree is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/sql_rt is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/sql is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/sql is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/xml_rt is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/xml is already defined
Dec 01, 2014 4:36:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/xml is already defined
Dec 01, 2014 4:36:10 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [296] milliseconds.
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/core_rt is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/core is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/core is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt_rt is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/fmt is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/functions is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/permittedTaglibs is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/scriptfree is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/sql_rt is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/sql is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/sql is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/xml_rt is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/xml is already defined
Dec 01, 2014 4:36:11 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/xml is already defined
log4j:WARN No appenders could be found for logger (com.opensymphony.xwork2.config.providers.XmlConfigurationProvider).
log4j:WARN Please initialize the log4j system properly.
Dec 01, 2014 4:36:12 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Dec 01, 2014 4:36:12 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Dec 01, 2014 4:36:12 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3311 ms
Dec 01, 2014 4:36:15 PM org.apache.jasper.compiler.TldLocationsCache tldScanJar
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 01, 2014 4:37:43 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/StrutsIntegHib] has started
Dec 01, 2014 4:37:43 PM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/StrutsIntegHib] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@31c2df]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@f0d523]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Dec 01, 2014 4:37:43 PM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/StrutsIntegHib] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@31c2df]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@13929d6]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Dec 01, 2014 4:37:43 PM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/StrutsIntegHib] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (value [com.opensymphony.xwork2.inject.ContainerImpl$10@31c2df]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@1b964b7]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/core_rt is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/core is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/core is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt_rt is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/fmt is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/functions is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/permittedTaglibs is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/scriptfree is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/sql_rt is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/sql is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/sql is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/xml_rt is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jstl/xml is already defined
Dec 01, 2014 4:37:45 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/xml is already defined
log4j:WARN No appenders could be found for logger (com.opensymphony.xwork2.config.providers.XmlConfigurationProvider).
log4j:WARN Please initialize the log4j system properly.
Dec 01, 2014 4:37:45 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/StrutsIntegHib] is completed

推荐答案

如果您使用的是模型驱动的操作,那么您直接从JSP访问的所有属性都应该在模型中而不是在操作类中进行聚合.创建类似

If you are using model driven action then all properties that you access directly from JSP should be aggregated in model, not in action class. Create a model like

public class TweetModel {

    private User user = new User();
    private List<User> userList = new ArrayList<User>();

    private boolean isAuthentic = false;

    private Tweet tweet = new Tweet();
    private List<Tweet> tweetList = new ArrayList<Tweet>();
                    //TweetDAO interface, TweetDAOImpl implements it.

    //getters and setters
    ...
}

动作类

public class TweetAction extends ActionSupport implements ModelDriven<TweetModel>, SessionAware{

    private TweetModel model = new TweetModel();
    private UserDAO userDAO = new UserDAOImpl(); //UserDAO interface, UserDAOImpl implements it.
    private TweetDAO  tweetDAO = new TweetDAOImpl();

    public TweetModel getModel() {
        return model;
    }   
    ...
}

在返回结果之前,您有责任在操作方法中填充tweetList.

It's your responsibility to populate tweetList in the action method before you return a result.

现在在JSP中,您可以使用类似的内容

Now in JSP you can use something like this

<table class="tweetTable" cellpadding="5px">
    <tr>            
        <th>Message</th>
        <th>Created</th>
    </tr> 

    <s:iterator value="tweetList">
        <tr class = "even">
            <td><s:property value="message" /></td>
            <td><s:property value="created" /></td>
        </tr>
    </s:iterator>

</table>

这篇关于数据库值未显示在网页中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆