私はJava-EEが初めてです(今日から)。私は本を読んでいて、同時に例をやっています。最初に、フォームを介してユーザー情報 (index.jsp 内) を取得し、output.jsp 内に表示する非常に単純なプログラムを作成しました。次に、Beans をモデルとして使用して、MVC ベースのアーキテクチャに変換しようとしました。したがって、ユーザーのデータは最初に ControllerServlet.java というサーブレットに送られ、データは Bean で SET され、ビュー (output.jsp) でデータは Bean から GET されます。しかし、Beans の不適切な使用に関して、エラーが発生します。
これが私のコードです:
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 を使っています。長く見えますが、正直なところ、初心者向けの基本的なコードです。私の問題は、Beans をまだよく知らないことだと思います。どうもありがとうございました