javax.el.PropertyNotFoundException:在 class [servlets.Controller] 上未找到名为 [resultRow] 的 public 静态字段
javax.el.PropertyNotFoundException: No public static field named [resultRow] was found on class [servlets.Controller]
我正在尝试使用填充有数据库条目的 ArrayList 来填充服务器上的 jsp 网页。我已经尝试了多种解决方案来让它工作,但没有任何结果。
我的变量是这样声明的:
Connection con = null;
Statement stmt = null;
private static ResultSet result = null;
Statement stmt2 = null;
private static ResultSet inventory = null;
public static ArrayList<String> dbEntries = new ArrayList<String>();
我有一个 init 方法,声明并 运行 这样:
public void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
new CreateCharacters();
new CreateInventories();
try
{
Properties props = new Properties();
props.load(new FileInputStream("WebContent" + File.separator + "files" + File.separator + "dbprops.properties"));
Class.forName(props.getProperty("Driver"));
con = DriverManager.getConnection(props.getProperty("URL"), props);
stmt = con.createStatement();
result = stmt.executeQuery("SELECT * FROM Characters_");
stmt2 = con.createStatement();
inventory = stmt2.executeQuery("SELECT * FROM Inventories_ AS i LEFT JOIN Characters_ AS c ON i.characterID=c.characterID");
while(result.next())
{
dbEntries.add(String.valueOf(result.getInt(1))); // characterID
dbEntries.add(result.getString(2)); // playerName
dbEntries.add(result.getString(3)); // characterName
dbEntries.add(String.valueOf(result.getInt(4))); // STR
dbEntries.add(String.valueOf(result.getInt(5))); // CON
dbEntries.add(String.valueOf(result.getInt(6))); // DEX
dbEntries.add(String.valueOf(result.getInt(7))); // INT
dbEntries.add(String.valueOf(result.getInt(8))); // WIS
dbEntries.add(String.valueOf(result.getInt(9))); // CHA
request.setAttribute("resultRow", dbEntries);
dbEntries.clear();
}
while(inventory.next())
{
dbEntries.add(String.valueOf(inventory.getInt(1))); // inventoryId
dbEntries.add(String.valueOf(inventory.getInt(2))); // characterId
dbEntries.add(inventory.getString(3)); // mainHand
dbEntries.add(inventory.getString(4)); // offHand
dbEntries.add(inventory.getString(5)); // armor
dbEntries.add(inventory.getString(6)); // carryMass
dbEntries.add(inventory.getString(7)); // contents
request.setAttribute("resultInventory", dbEntries);
dbEntries.clear();
}
RequestDispatcher dispatch = request.getRequestDispatcher("/index.jsp");
dispatch.forward(request, response);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
这应该创建两个作用域变量 - resultRow 和 resultInventory。
这是导致问题的 JSP 片段:
<tr class = dbCharacters>
<td><input type=text name='ID${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[0]}'></td>
<td><input type=text name='Player${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[1]}'></td>
<td><input type=text name='Character${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[2]}'></td>
<td><input type=text name='Strength${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[3]}'></td>
<td><input type=text name='Constitution${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[4]}'></td>
<td><input type=text name='Dexterity${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[5]}'></td>
<td><input type=text name='Intelligence${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[6]}'></td>
<td><input type=text name='Wisdom${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[7]}'></td>
<td><input type=text name='Charisma${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[8]}'></td>
<td><button onclick = 'ShowInventory(${Controller.resultRow[0]})'>Inventory</button></td>
<td><input type=submit form = 'currentCharacter' value=Update name='Update${Controller.resultRow[0]}'></td>
<td><input type=submit form = 'currentCharacter' value=Delete name='Delete${Controller.resultRow[0]}'></td>
</tr>
然而,我的 jsp 只是拒绝读取 resultRow 和 resultInventory。我已经尝试了多种解决方案来让它工作:
- 将初始化代码移动到 doGet() 方法具有相同的结果。
- 单独使用 "dbEntries" 不会导致错误,但表格是空白的,即使删除了 dbEntries.clear() 语句也是如此。
更新:我想我已经推断出错误的原因。当我使用 Controller.resultRow/resultInventory 时,JSP EL 试图找到一个名为 resultRow/resultInventory 的实际变量。但是,该属性并没有这样存储;删除 "Controller" 前缀似乎已经解决了异常。但是,应该存在的信息是空白的。
问题已解决。如果你有 运行 遇到和我一样的问题,请看这里:
我做错的是我直接从浏览器加载 JSP,这意味着我的 Servlet 甚至没有被调用!为了解决这个问题,我将 jsp 的名称更改为 "output.jsp"(字面上可以是任何名称),然后创建一个启动页面 (index.html)。然后写入 html 以向 Servlet 发送 GET 请求,然后将其分派到 JSP.
我正在尝试使用填充有数据库条目的 ArrayList 来填充服务器上的 jsp 网页。我已经尝试了多种解决方案来让它工作,但没有任何结果。
我的变量是这样声明的:
Connection con = null;
Statement stmt = null;
private static ResultSet result = null;
Statement stmt2 = null;
private static ResultSet inventory = null;
public static ArrayList<String> dbEntries = new ArrayList<String>();
我有一个 init 方法,声明并 运行 这样:
public void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
new CreateCharacters();
new CreateInventories();
try
{
Properties props = new Properties();
props.load(new FileInputStream("WebContent" + File.separator + "files" + File.separator + "dbprops.properties"));
Class.forName(props.getProperty("Driver"));
con = DriverManager.getConnection(props.getProperty("URL"), props);
stmt = con.createStatement();
result = stmt.executeQuery("SELECT * FROM Characters_");
stmt2 = con.createStatement();
inventory = stmt2.executeQuery("SELECT * FROM Inventories_ AS i LEFT JOIN Characters_ AS c ON i.characterID=c.characterID");
while(result.next())
{
dbEntries.add(String.valueOf(result.getInt(1))); // characterID
dbEntries.add(result.getString(2)); // playerName
dbEntries.add(result.getString(3)); // characterName
dbEntries.add(String.valueOf(result.getInt(4))); // STR
dbEntries.add(String.valueOf(result.getInt(5))); // CON
dbEntries.add(String.valueOf(result.getInt(6))); // DEX
dbEntries.add(String.valueOf(result.getInt(7))); // INT
dbEntries.add(String.valueOf(result.getInt(8))); // WIS
dbEntries.add(String.valueOf(result.getInt(9))); // CHA
request.setAttribute("resultRow", dbEntries);
dbEntries.clear();
}
while(inventory.next())
{
dbEntries.add(String.valueOf(inventory.getInt(1))); // inventoryId
dbEntries.add(String.valueOf(inventory.getInt(2))); // characterId
dbEntries.add(inventory.getString(3)); // mainHand
dbEntries.add(inventory.getString(4)); // offHand
dbEntries.add(inventory.getString(5)); // armor
dbEntries.add(inventory.getString(6)); // carryMass
dbEntries.add(inventory.getString(7)); // contents
request.setAttribute("resultInventory", dbEntries);
dbEntries.clear();
}
RequestDispatcher dispatch = request.getRequestDispatcher("/index.jsp");
dispatch.forward(request, response);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
这应该创建两个作用域变量 - resultRow 和 resultInventory。
这是导致问题的 JSP 片段:
<tr class = dbCharacters>
<td><input type=text name='ID${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[0]}'></td>
<td><input type=text name='Player${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[1]}'></td>
<td><input type=text name='Character${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[2]}'></td>
<td><input type=text name='Strength${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[3]}'></td>
<td><input type=text name='Constitution${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[4]}'></td>
<td><input type=text name='Dexterity${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[5]}'></td>
<td><input type=text name='Intelligence${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[6]}'></td>
<td><input type=text name='Wisdom${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[7]}'></td>
<td><input type=text name='Charisma${Controller.resultRow[0]}' form ='currentCharacter' value='${Controller.resultRow[8]}'></td>
<td><button onclick = 'ShowInventory(${Controller.resultRow[0]})'>Inventory</button></td>
<td><input type=submit form = 'currentCharacter' value=Update name='Update${Controller.resultRow[0]}'></td>
<td><input type=submit form = 'currentCharacter' value=Delete name='Delete${Controller.resultRow[0]}'></td>
</tr>
然而,我的 jsp 只是拒绝读取 resultRow 和 resultInventory。我已经尝试了多种解决方案来让它工作:
- 将初始化代码移动到 doGet() 方法具有相同的结果。
- 单独使用 "dbEntries" 不会导致错误,但表格是空白的,即使删除了 dbEntries.clear() 语句也是如此。
更新:我想我已经推断出错误的原因。当我使用 Controller.resultRow/resultInventory 时,JSP EL 试图找到一个名为 resultRow/resultInventory 的实际变量。但是,该属性并没有这样存储;删除 "Controller" 前缀似乎已经解决了异常。但是,应该存在的信息是空白的。
问题已解决。如果你有 运行 遇到和我一样的问题,请看这里:
我做错的是我直接从浏览器加载 JSP,这意味着我的 Servlet 甚至没有被调用!为了解决这个问题,我将 jsp 的名称更改为 "output.jsp"(字面上可以是任何名称),然后创建一个启动页面 (index.html)。然后写入 html 以向 Servlet 发送 GET 请求,然后将其分派到 JSP.