2016-06-24 11 views
1

私はjspを初めて使用しています。私はuseBeanの方法を学んでいた、とクラスを型(jsp)に解決できません

http://www.studytonight.com/jsp/getproperty-tag.php

の例を下にスクロールしがあり、あなたはそれを見つけるでしょう。ここで

は私のJSPファイルです:

import java.io.Serializable; 
public class PersonBean implements Serializable 
{ 
private String name="Hello World"; 
    public PersonBean() 
    { 
    this.name="Hello"; 
    } 
    public void setName(String name) 
    { 
    this.name = name; 
    } 
    public String getName() 
    { 
    return name; 
    } 
} 

<html> 
<head> 
    <title>Welcome Page</title> 
</head> 
<jsp:useBean id="person" type="PersonBean" scope="request"/> <body> 
    Hello, <jsp:getProperty name="person" property="name"/>  </body> </html> 

とここに私のPersonBean.javaファイル、クラスファイルにコンパイルし、 クラスファイル内に置かれています

エラーが発生しました: エラーが発生しましたライン:/hello1.jsp PersonBeanがタイプ

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 5 in the jsp file: /hello1.jsp 
PersonBean cannot be resolved to a type 
2:  <head> 
3:   <title>Welcome Page</title> 
4:  </head> 
5:  <jsp:useBean id="person" type="PersonBean" scope="request"/> 
6: <body> 
7:   Hello, <jsp:getProperty name="person" property="name"/> 
8: </body> 


An error occurred at line: 5 in the jsp file: /hello1.jsp 
PersonBean cannot be resolved to a type 
2:  <head> 
3:   <title>Welcome Page</title> 
4:  </head> 
5:  <jsp:useBean id="person" type="PersonBean" scope="request"/> 
6: <body> 
7:   Hello, <jsp:getProperty name="person" property="name"/> 
8: </body> 


An error occurred at line: 7 in the jsp file: /hello1.jsp 
PersonBean cannot be resolved to a type 
4:  </head> 
5:  <jsp:useBean id="person" type="PersonBean" scope="request"/> 
6: <body> 
7:   Hello, <jsp:getProperty name="person" property="name"/> 
8: </body> 
9: </html> 


An error occurred at line: 7 in the jsp file: /hello1.jsp 
PersonBean cannot be resolved to a type 
4:  </head> 
5:  <jsp:useBean id="person" type="PersonBean" scope="request"/> 
6: <body> 
7:   Hello, <jsp:getProperty name="person" property="name"/> 
8: </body> 
9: </html> 


Stacktrace: 
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103) 
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366) 
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:490) 
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:379) 
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:354) 
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:341) 
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.70 logs. 

答えて

0

のjspに解決することはできません:useBeanのアクションタグ:

<jsp:useBean id= "instanceName" scope= "page | request | session | application" 
    class= "packageName.className" type= "packageName.className" 
    beanName="packageName.className | <%= expression >" > 
</jsp:useBean> 

1.Put PersonBeanクラスのJSPファイル内5パッケージを別のクラスで使用できるようにする必要があります。デフォルトのパッケージ内のクラスは、それ自体がパッケージ内にあるクラスには見えません。

2. .classファイルをweb-inf/classesに設定します。

<jsp:useBean/>宣言のPersonBeanの完全修飾クラス名を指定します。

さらに、デフォルトのパッケージクラスを見つけるJVMの規約は、「現在の」ディレクトリを調べることです。サーバー環境では、現在のディレクトリを制御できないため、パッケージを使用する必要があります。

は、以下のようにコードを変更:

PersonBean.java

package com.person; 

import java.io.Serializable; 

    public class PersonBean implements Serializable { 

     private String name="Hello World"; 
     public PersonBean() 
     { 
     this.name="Hello"; 
     } 
     public void setName(String name) 
     { 
     this.name = name; 
     } 
     public String getName() 
     { 
     return name; 
     } 
    } 

Person.jsp

<html> 
<head> 
    <title>Welcome Page</title> 
</head> 
<jsp:useBean id="person" class="com.person.PersonBean" scope="request"/>  
Hello, <jsp:getProperty name="person" property="name"/> 
</html> 
+0

私はそれの多くを理解していませんでした。最初の点について詳しく説明できますか? –

+0

@Karan Thakkarどのパッケージに 'PersonBean'クラスを追加しましたか? – Mihir

+0

classes = "action.PersonBean" PersonBean.classファイルをWEB-INF/classes&WEB-INF/classes/actionに入れてください。パッケージアクションを追加しました。 WEB-INF/classes/actionにあるjavaファイルにコピーしてコンパイルします。 –

関連する問題