Eclipse和JSP编程。在哪里存储类? [英] Eclipse and JSP programing. Where to store classes?

查看:193
本文介绍了Eclipse和JSP编程。在哪里存储类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习一些使用Eclipse的JSP编程,我遇到了一些问题:



这是我有的:

1)在WebContent文件夹中有test.jsp文件,只显示Hello World!。我选择文件并单击运行方式。问我重新启动Tomcat服务器。点击Yes,页面在Eclipse中打开,显示Hello World!。

2)我写了Car类并将其存储在Java Resources / src中

3)尝试引用Car类我的测试JSP和一切似乎是对的。 (代码如下)

 <%@ page import =ws.example。*%& 
<%
car myCar = new car(dodge,neon);
out.println(myCar.getMake());
&>

4)再次单击运行方式,我收到了Hello World消息。点击刷新,错误出现了那辆车MyCar =新车(闪避,霓虹灯);无法解析为类型。

5)再次单击刷新2次,第二次消息Hello World!。显示了。

6)保持点击刷新按钮,有时我会有错误,有时Hello World。我知道Hello World是来自现金,但我如何避免这一点?很烦人。



问题:

1)当我在项目中进行更改时,我如何确保这些更改包含在我启动项目?

2)哪里是存储你的课程的好地方?显然我尝试的没有工作。



UPDATED:



我的类:

  package ppp; 

public class MyCar {

String make;
字符串模型;

public MyCar(String make,String model){
this.make = make;
this.model = model;
}

public String getMake()
{
return make;

}
public String getModel()
{
return model;
}
}

这里是我的JSP代码(NewFile.jsp):

 <%
ppp.MyCar car = new ppp.MyCar(,);
%>

项目结构:

  TestProject 
- 部署描述符:TestProject
- Java资源
- src
- MyCar.java
- 库
- .. 。
- JavaScript资源
- build
- WebContent
- META-INF
- WEB-INF
- NewFile.jsp



我在这里看不到任何错误。



感谢

解决方案


1)当我更改项目时,确保在我开始项目时包含这些更改。


重新发布服务器。这是服务器服务器视图中的右键菜单选项。如果无效,请重新构建项目并清理服务器。您可以在服务器属性中配置自动发布。





服务器如何操作取决于服务器的make / version和插件。例如,Tomcat是一个糟糕的发布商,你宁愿只是重新启动服务器。



顺便说一下,我从来不使用 Run as 选项,也不使用Eclipse的内置浏览器。它被认为是一个穷人。我只是启动服务器,然后在我自己喜欢的webbrowser(例如Firefox,Chrome等)打开页面。







2)哪里是存储你的课程的好地方?显然我尝试的没有工作。


只是在运行时类路径中的一个包中常用的方式。 / p>




无关:尝试处理您的命名约定(类名以大写开头),并尝试不要在JSP中放置Java代码(这是一个很差的做法)。


I am trying to learn some JSP programming with Eclipse and I am having some issues:

Here is what I have:
1) Have test.jsp file inside WebContent folder that simply displays "Hello World!". I selected file and clicked Run As. Asked me to restart Tomcat server. Clicked Yes and page opened inside Eclipse showing "Hello World!".
2) I wrote Car class and stored it inside Java Resources/src
3) Tried to reference Car class in my test JSP and everything seemed right. (code below)

<%@ page import="ws.example.*" %>
<%  
    car myCar = new car("dodge", "neon");
    out.println(myCar.getMake());
&>

4) Clicked "Run As" again and I got Hello World message. Clicked refresh, error showed up that car MyCar = new car("dodge", "neon"); cannot be resolved to a type.
5) Clicked Refresh again 2 times and second time message "Hello World!" showed up.
6) Kept clicking Refresh button and sometime I would have error, sometime Hello World. I understand that Hello World is coming from cash, but how can I avoid this? Very annoying.

Questions:
1) When I make change in my project, how can I make sure those changes are included when I start the project?
2) Where is the good place to store your classes? Obviously what I try didn't work.

UPDATED:

Here is my class:

package ppp;

public class MyCar {

    String make;
    String model;

    public MyCar(String make, String model) {
        this.make = make;
        this.model = model;
    }

    public String getMake()
    {
        return make;

    }
    public String getModel()
    {
        return model;
    }
}

here is my JSP code (NewFile.jsp):

<%
    ppp.MyCar car = new ppp.MyCar("", "");
%>

Project Structure:

 TestProject  
     - Deployment Descriptor: TestProject  
     - Java Resources  
         - src  
             - MyCar.java   
         - Libraries  
             - ...  
     - JavaScript Resources    
     - build  
     - WebContent  
         - META-INF
         - WEB-INF
         - NewFile.jsp

I just don't see any room for error here.

thanks

解决方案

1) When I make change in my project, how can I make sure those changes are included when I start the project?

Republish the server. It's an rightclick menu option of the server in Servers view. If in vain, rebuild the project and clean the server. You can configure automatic publishing in the server's properties.

How servers act on this depends on the server make/version and the plugin. For example, Tomcat is a poor publisher, you'd rather like to just restart the server. Glassfish with its plugin is a tremendous publisher, it basically happens realtime.

By the way, I never use Run as option nor Eclipse's builtin browser. It's known to be a poor one. I just start the server and then open the page in my own favourite webbrowser (e.g. Firefox, Chrome, etc).


2) Where is the good place to store your classes? Obviously what I try didn't work.

Just in a package somewhere in the runtime classpath the usual way.


Unrelated to the concrete problem: try working on your naming conventions (classnames should start with uppercase) and try to avoid putting Java code in JSP (it's a very poor practice).

这篇关于Eclipse和JSP编程。在哪里存储类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆