【发布时间】:2015-01-29 08:33:09
【问题描述】:
我在我的项目中使用 Struts 2 + Hibernate。不过,我对这个领域还比较陌生。我能够在 Java 代码中获取所需的值,但无法进入 JSP 页面。
这是我的struts.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() 方法。
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 界面:
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:
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;
}
}
我能够获得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]
所以courses 中至少有一些东西。但是在 JSP 页面中,它进入了else 循环。
哪里出错了?
这是我的堆栈跟踪:
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
【问题讨论】:
-
请发布您的堆栈跟踪。
-
堆栈跟踪中没有异常或错误。发布了堆栈跟踪。
-
退出使用 ModelDriven,尤其是如果你不明白它的作用。如果你的模型是
Tweet tweet而你的动作是ModelDrivet<Tweet>,你怎么能假装阅读List<Tweet> tweetList?
标签: java spring hibernate jsp struts2