为什么deos Weblogic日志说当我尝试在jsp文件中使用bean类对象时,无法解析bean?

时间:2018-01-11 12:29:50

标签: java jsp servlets javabeans

我试图在java中使用MVC架构创建一个简单的电子商务网站。最初我创建了一个名为TestingServlet的控制器servlet,一个web.xml文件,一个名为DbBean的classes文件夹下的com.bean包中的bean类,以及一个Default.jsp,Header.jsp,Menu.jsp。我编译了bean和servlet类。最后,当我在WebLogic服务器上部署应用程序并运行它时,我收到以下错误 -

the WebLogic log

正如您在日志中所看到的,它表示无法编译JSP JSP / Menu.jsp,这意味着问题必须与Menu.jsp有关。而且,它说第28行bean无法解决。所以,我检查了部分代码,但它对我来说很好看。是什么导致了这个问题?

以下是我制作的所有文件 - TestingServlet.java

import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.bean.DbBean;
public class TestingServlet extends HttpServlet
{
  public void init(ServletConfig conf)
   {
     System.out.println("Initiallizing ControllerServlet");
     ServletContext ctx = conf.getServletContext();

     ctx.setAttribute("base",conf.getInitParameter("base"));
     System.out.println("base = " +conf.getInitParameter("base"));
     ctx.setAttribute("imageUrl",conf.getInitParameter("imageUrl"));
     System.out.println("imageUrl = " +conf.getInitParameter("imageUrl"));


      DbBean bean = new DbBean();
      bean.setDbUrl(conf.getInitParameter("dbUrl"));
      bean.setDbUserName(conf.getInitParameter("userName"));
      bean.setDbPassword(conf.getInitParameter("password"));

      ctx.setAttribute("bean",bean);
      System.out.println("bean object successfully made,its properties set 
                           and set in app scope..");

      try
      {
        Class.forName(conf.getInitParameter("jdbcDriver"));
        System.out.println("Driver Class Loaded Successfully");
      }
      catch(Exception e)
          {
            System.out.println("Could not load the driver class");
          }
   }
   protected void doGet(HttpServletRequest req , HttpServletResponse res) 
                 throws ServletException,IOException
   {
     doPost(req,res);
     System.out.println("in doGet method");
   }
   protected void doPost(HttpServletRequest req , HttpServletResponse res) 
                  throws ServletException,IOException
   {  
     System.out.println("in doPost method");
      String base = "/jsp/";
     String url = base + "Default.jsp";
     String action = req.getParameter("action");

     if(action!=null)
     {
      if(action.equals("search"))
         url = base + "SearchResults.jsp";
       else if(action.equals("browseCatalog"))
               url = base + "BrowseCatalog.jsp";
       if(action.equals("productDetails"))
          url = base + "ProductDetails.jsp";
       if(action.equals("addShoppingItem") || 
          action.equals("updateShoppingItem") || 
          action.equals("deleteShoppingItem") || 
          action.equals("displayShoppingCart"))
        url = base + "ShoppingCart.jsp";
      if(action.equals("checkOut"))
        url = base + "CheckOut.jsp";
      if(action.equals("order"))
        url = base + "Order.jsp";
     }
     System.out.println("if part successfully executed");
     RequestDispatcher rd = req.getRequestDispatcher(url);
     System.out.println("RD object made successfully..");
     rd.forward(req,res);

     System.out.println("forward successfully executed..");
 }
}

DbBean.java

package com.bean;
import java.sql.*;
import java.util.Hashtable;

public class DbBean
  {
    public String dbUrl = "";
    public String dbUserName = "";
    public String dbPassword = "";

    public void setDbUrl(String url)
      {
        dbUrl = url;
      }
    public void setDbUserName(String userName)
      {
        dbUserName = userName;
      }
    public void setDbPassword(String password)
      {
         dbPassword = password;
      }

     public Hashtable getCategories()
      {
        Hashtable categories = new Hashtable();
        try
        {
          Connection conn = 
          DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
          Statement stmt = conn.createStatement();
          String sql = "select CategoryId,Category from Categories" +" ";
          ResultSet rs = stmt.executeQuery(sql);
          while(rs.next())
          {
            categories.put(rs.getString(1),rs.getString(2));
          }
          rs.close();
          stmt.close();
          conn.close();
        }
        catch(SQLException e)
        {}
        return categories;
      }

    } 

的Default.jsp

<html>
<head>
 <title>Welcome</title>
</head>
<body>
<table>
 <tr>
    <td colspan="2">
        <jsp:include page="Header.jsp" flush = "true"/>
    </td>
 </tr>
 <tr>
   <td>
      <jsp:include page="Menu.jsp" flush="true"/>
   </td>
   <td valign="top">
      <H2>WELCOME TO MY E-MALL.</H2>
   </td>
 </tr>
</table>
</body>
</html>

引入了menu.jsp

<%@ page import="java.util.*" %>
<jsp : useBean id = "bean" scope = "application" class="com.bean.DbBean" />

<%
   String base = (String)application.getAttribute("base");
%>
<table cellspacing="0" cellpadding="5" width="150" border="0">
  <tr>
     <td bdcolor="F6F6F6">
        <font face="Verdana">Search</font>
        <form>
           <input type="hidden" name="action" value="search">
           <input type="text" name="keyword" size="10">
           <input type="submit" value="Go">
        </form>
     </td>
  </tr>
  <tr >
     <td bgcolor="F6F6F6">
       <font face="Verdana">
          Categories:
       </font>
     </td>
  </tr>
  <tr valign="top">
     <td bgcolor="F6F6F6">
        <%
          Hashtable categories = bean.getCategories();
          Enumeration categoryIds = categories.keys();
          while(categoryIds.hasMoreElements())
          {
             Object categoryId = categoryIds.nextElement();
             out.println("<a href=" +base +"?
                 action=browseCatalog&categoryId=" +categoryId.toString() 
                 +">" +categories.get(categoryId) +"</a><br>"); 
          }
        %>
     </td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:0)

jsp:useBean用于Java Bean的对象。

是否DbBean不遵守JavaBeans约定,即查看What is a JavaBean exactly?,也许您可​​以更改DBean以遵守约定,或者您可以放弃使用jsp:useBean并且只是在scriptlet中实现并操纵它?