我是 Java-EE 的新手(从今天开始)。我一边看书一边做例子。起初,我编写了一个非常简单的程序,它通过表单获取用户信息(在 index.jsp 中),然后在 output.jsp 中显示它们。然后我尝试将其转换为基于 MVC 的架构,使用 Beans 作为模型。因此,用户的数据首先进入一个名为 ControllerServlet.java 的 servlet,数据在 Bean 中是 SET,在视图 (output.jsp) 中数据是从 bean 中获取。但它给了我关于错误使用 Beans 的错误。
这是我的代码:
索引.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。非常感谢