<jsp:usebean>错误&#34;找不到符号&#34;在JSP编译期间</jsp:usebean>

时间:2012-12-08 18:59:49

标签: jsp servlets javabeans

我是Java-EE的新手(从今天开始)。我正在读书并同时做例子。起初我写了一个非常简单的程序,它通过一个表单获取用户信息(在index.jsp中),然后在output.jsp中显示它们。 然后我尝试将它转换为基于MVC的架构,使用Beans作为模型。因此,用户的数据首先转到名为ControllerServlet.java的servlet,数据在Bean中是SET,而在视图(output.jsp)中,数据是来自bean的GET。但它给了我关于错误使用豆子的错误。

这是我的代码:

的index.jsp

    <%-- 
    Document   : index
    Created on : Dec 8, 2012, 4:16:48 PM
    Author     : mohsen
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Developer survey</title>
    </head>
    <body>
        <h1>Welcome to SHIT</h1>
        <p>Please indicate shit, so we could be able to do further shit later.</p>
        <form action="output.jsp">
            <table border="0">
                <tbody>
                    <tr>
                        <td>Full Name:</td>
                        <td><input type="text" name="fullName" value="" /></td>
                    </tr>
                    <tr>
                        <td>Java</td>
                        <td><input type="checkbox" name="progLang" value="Java" /></td>
                    </tr>
                    <tr>
                        <td>Groovy</td>
                        <td><input type="checkbox" name="progLang" value="Groovy" /></td>
                    </tr>
                    <tr>
                        <td>Scala</td>
                        <td><input type="checkbox" name="progLang" value="Scala" /></td>
                    </tr>
                    <tr>
                        <td>C#</td>
                        <td><input type="checkbox" name="progLang" value="C#" /></td>
                    </tr>
                    <tr>
                        <td>Ruby</td>
                        <td><input type="checkbox" name="progLang" value="Ruby" /></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><input type="submit" value="Submit" /></td>
                    </tr>
                </tbody>
            </table>

        </form>
    </body>
</html>

这是SurveyData.java(Bean)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.beans.*;
import java.io.Serializable;

/**
 *
 * @author mohsen
 */
public class SurveyData implements Serializable {

    public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";
    private String fullName;
    private String[] progLang;
    private PropertyChangeSupport propertySupport;

    public SurveyData() {
        propertySupport = new PropertyChangeSupport(this);
    }

    public String getFullName() {
        return fullName;
    }

    public String[] getProgLang() {
        return progLang;
    }

    public void setProgLang(String[] value) {
        String[] oldValue = progLang;
        progLang = value;
        propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, progLang);
    }

    public void setFullName(String value) {
        String oldValue = fullName;
        fullName = value;
        propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, fullName);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }
}

这是ControllerServlet.java:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author mohsen
 */
@WebServlet(name = "ControllerServlet", urlPatterns = {"/ControllerServlet"})
public class ControllerServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        SurveyData surveyData = new SurveyData();
        surveyData.setFullName(request.getParameter("fullName"));
        surveyData.setProgLang(request.getParameterValues("progLang"));
        request.setAttribute("surveyData", surveyData);

        request.getRequestDispatcher("output.jsp").forward(request, response);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

最后,这是output.js:

<%-- 
    Document   : output
    Created on : Dec 8, 2012, 4:42:59 PM
    Author     : mohsen
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<jsp:useBean id="surveyData" scope="request" class="SurveyData" />
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Thanks for doing the shit we asked you!</h1>
        <P>
            <%--=request.getParameter("fullName")--%>,
            <jsp:getProperty name="surveyData" property="fullName" />
            you have said that you know these shit:
        </P>
        <ul>
            <% 
                //String[] selectedLanguages = request.getParameterValues("progLang");
                String[] selectedLanguages = surveyData.getProgLang();
                if (selectedLanguages != null){
                    for(int i = 0; i < selectedLanguages.length; i++){
                        %>
                        <li>
                            <%= selectedLanguages[i]%>
                        </li>
                        <%
                    }
                }
            %>
        </ul>
    </body>
</html>

这些是输出错误:

Compiling 1 source file to C:\Users\mohsen\Documents\NetBeansProjects\simplewebapp\build\generated\classes
C:\Users\mohsen\Documents\NetBeansProjects\simplewebapp\build\generated\src\org\apache\jsp\output_jsp.java:47: cannot find symbol
symbol  : class SurveyData
location: class org.apache.jsp.output_jsp
      SurveyData surveyData = null;
      ^
C:\Users\mohsen\Documents\NetBeansProjects\simplewebapp\build\generated\src\org\apache\jsp\output_jsp.java:49: cannot find symbol
symbol  : class SurveyData
location: class org.apache.jsp.output_jsp
        surveyData = (SurveyData) _jspx_page_context.getAttribute("surveyData", PageContext.REQUEST_SCOPE);
C:\Users\mohsen\Documents\NetBeansProjects\simplewebapp\build\generated\src\org\apache\jsp\output_jsp.java:51: cannot find symbol
symbol  : class SurveyData
location: class org.apache.jsp.output_jsp
          surveyData = new SurveyData();
C:\Users\mohsen\Documents\NetBeansProjects\simplewebapp\build\generated\src\org\apache\jsp\output_jsp.java:67: cannot find symbol
symbol  : class SurveyData
location: class org.apache.jsp.output_jsp
      out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((SurveyData)_jspx_page_context.findAttribute("surveyData")).getFullName())));
                                                                        ^
4 errors
C:\Users\mohsen\Documents\NetBeansProjects\simplewebapp\nbproject\build-impl.xml:930: The following error occurred while executing this line:
C:\Users\mohsen\Documents\NetBeansProjects\simplewebapp\nbproject\build-impl.xml:284: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 1 second)

顺便说一句,我正在使用NetBeans。 它似乎很长,但老实说它只是一些基本的noobie代码。我想我的问题是我还不太了解Beans。 非常感谢你

1 个答案:

答案 0 :(得分:1)

默认包中的类对于包本身内的类是不可见的。

亲自试验:

// No package!

public class Foo {
}
package com.example;

public class Bar {
    public static void main(String... args) {
        Foo foo = new Foo(); // compile fail, cannot find symbol
    }
}

这与<jsp:useBean>正在努力解决的问题完全相同。如果您希望它们可见(并因此可导入)到外部,请将所有公共类放在一个包中。


对具体问题

无关,您根本不需要<jsp:useBean>。您已经使用servlet创建并设置了bean。完全删除<jsp:useBean>标记。另外请确保您正在阅读正确的教程。 <jsp:useBean>仅用于无控制器(read:servlet-less)MVC方法。另请参阅How to pass parameters from Servlet via Bean to JSP page with the help of Session?Our servlets wiki page

相关问题