我使用Struts 2和顯示標記表格式化工具來顯示數據庫查詢結果(SELECT *查詢)。我遇到了一個奇怪的錯誤。在從Add.jsp向數據庫提交記錄之後,它將我帶到View.jsp,其中表格正確顯示。但是,如果我直接轉到View.jsp,或者通過Add.jsp頁面中的超鏈接而不向數據庫提交條目,它似乎不會執行將數據庫條目檢索到變量以使表顯示的必要方法。在這種情況下,我最終會得到一個短語,即「應該顯示的內容」。Struts 2 DisplayTag僅在提交表單時才顯示DB信息表
我被告知這很可能是我的方法名稱和Struts 2方面的問題,而不是Java方面的任何功能。我對Struts非常陌生,並希望在識別和修復錯誤方面有所幫助。一些代碼摘錄如下,但如果需要,我可以發佈整個內容。另外 - 當View.jsp加載後,無論是在表單提交後還是通過直接鏈接都不顯示錯誤。
Add.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Add Development Environment</title>
</head>
<body>
<div id="main">
<h2>Add Development Environment</h2>
<p><s:a href="View.jsp">View Entries</s:a></p>
<s:actionerror />
<s:form action="doEntries.action" method="post" validate="true">
<s:textfield name="OS" key="label.OS" size="20" />
<s:textfield name="OSVersion" key="label.OSVersion" size="20" />
<s:textfield name="Note" key="label.note" size="20" />
<s:submit method="doEntries" key="label.submit" />
</s:form>
</div>
</body>
</html>
view.jsp的
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="display" uri="http://displaytag.sf.net" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/"WebContent/View.jsp"DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>View Development Environments</title>
</head>
<body>
<div id="main">
<h2>View Development Environments</h2>
<p><s:a href="Add.jsp">Add Entry</s:a></p>
<!--<s:set name="items" value="entries" scope="request"/>-->
<display:table name="entries" class="DevEnvironment" requestURI="" id="itemsList" export="false" pagesize="15">
<display:column property="did" title="id"/>
<display:column property="OS" title="Operating System"/>
<display:column property="OSVersion" title="Operating System Version" />
<display:column property="note" title="Note"/>
</display:table>
</div>
</body>
</html>
DatabaseAction.java
public class DatabaseAction extends ActionSupport{
private static final Logger logger = Logger.getLogger(DatabaseAction.class);
DBO myDBO;
private String OS;
private String OSVersion;
private String note;
private ArrayList<DevEnvironment> entries;
private double offset;
private double limit;
public DatabaseAction(){
super();
//connect to DB
myDBO = new DBO("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/devenvironments?user=root&password=mysqliscool");
}
public String doEntries(){
logger.info("puting stuff in DB! =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-==-=-=");
if(myDBO.connect() && myDBO.setEntry(OS, OSVersion, note)){
return "success";
} else {
return "error";
}
}
public ArrayList<DevEnvironment> getEntries() {
logger.info("-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=getEntries called");
entries = myDBO.getEntry(0, -1);
return entries;
}
public void setEntries(ArrayList<DevEnvironment> entries){
this.entries = entries;
}
public String retrieveEntries(){
logger.info("-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=retrieveEntries called");
if(myDBO.connect()){
logger.info("-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=EXECUTING SQL SELECT METHOD");
//entries = myDBO.getEntry(0, -1);
//offset, limit
return "success";
} else {
return "input";
}
}
struts.xml的
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="default" extends="struts-default" namespace="/">
<action name="doEntries" method="doEntries" class="net.josh.devenvironments.DatabaseAction" >
<result name="success">View.jsp</result>
<result name="input">Add.jsp</result>
</action>
<!--
<action name="retrieveEntries" method="retrieveEntries" class="net.josh.devenvironments.DatabaseAction" >
<result name="success">View.jsp</result>
<result name="input">Add.jsp</result>
</action>
-->
</package>
</struts>
My * .jsp文件全部位於WEB-INF目錄下,但我仍然可以公開訪問它們。 我想我通過改變從View.jsp到view.action(從Add.jsp頁面)的鏈接解決了這個問題......但是現在Struts決定打破自己,這是另一篇文章,我想。 感謝您的幫助! – Tasyne 2011-02-02 21:40:22