Netbeans project with UTF-8

基本上,NetBeans預設的file encoding是系統語系,所以在繁中版Windows是MS950,遇到”堃”、”綉”等字時會發生錯誤。鳥毅這幾年都用UTF-8編碼,必須手動改 Tools->Options->Advanced Options->Editing->Java Sources->Default EncodingUTF-8。但這樣編譯時會錯,因此還得改Project的Properties->Build->Compiling->Additional Compiler Options加上 -encoding UTF-8

來源鳥毅的Blog

Advertisements

Icefaces Application on Tomcat 6 using Netbeans

Similar to the portlet development but simpler.

download latest icefaces 1.7 RC1

pack these in library manager as Icefaces 1.7 library

backport-util-concurrent.jar

commons-beanutils.jar

commons-digester.jar

commons-discovery.jar

commons-fileupload.jar

commons-logging.jar

icefaces.jar

icefaces-comps.jar

Create a web application project and use JavaServerFace framework.

include Icefaces 1.7 library that we create.

Add these to your web.xml

   <servlet>
        <servlet-name>Persistent Faces Servlet</servlet-name>
        <servlet-class>
            com.icesoft.faces.webapp.xmlhttp.PersistentFacesServlet
        </servlet-class>
        <load-on-startup> 1 </load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>Blocking Servlet</servlet-name>
        <servlet-class>com.icesoft.faces.webapp.xmlhttp.BlockingServlet</servlet-class>
        <load-on-startup> 1 </load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Persistent Faces Servlet</servlet-name>
        <url-pattern>*.iface</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Persistent Faces Servlet</servlet-name>
        <url-pattern>/xmlhttp/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Blocking Servlet</servlet-name>
        <url-pattern>/block/*</url-pattern>
    </servlet-mapping>

Add a jspx to you project, if you put that under Web pages/jspx/foo.jspx

You will need to use it by http://localhost:8080/projectName/jspx/foo.iface

you are all set.

Enjoy!

Develop JSF1.2 with Netbeans 5.5.1 running on Tomcat 6 with Mysql backend

My Environment

JSF 1.2

Netbeans 5.5.1

Tomcat 6

Mysql 5

Prepare Tomcat 6

Download and unpack Tomcat 6.

Run: [tomcat6]\bin\startup.bat in windows

Shutdowm: [tomcat6]\bin\shutdown.bat in windows

Change port: [tomcat6]\apache-tomcat-6.0.16\conf\server.xml

Add user: [tomcat6]\apache-tomcat-6.0.16\conf\tomcat-user.xml

<role rolename="manager"/> <user username="tomcat" password="s3cret" roles="manager"/>

Open server manager in Netbeans-> Add Server, choose Tomcat 5.5(I don’t see Tomcat 6 and don’t know how to add it). Catalina Home means where you unpack Tomcat 6. username/password is the manager username/password we set previously.

You can run and deploy project using Tomcat 6 by now.

Prepare Mysql

Download and run it as a service from Mysq which will be trivial to teach.

Copy JDB-connecter for Mysql under [tomcat6]\lib

Add the following setting in your context

<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"
removeAbandoned="true" emoveAbandonedTimeout="60"/>
</Context>

Context for tomcat 6 can be in the following ways!

  • in the $CATALINA_HOME/conf/context.xml file: the Context element information will be loaded by all webapps
  • in the $CATALINA_HOME/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all webapps of that host
  • in individual files (with a “.xml” extension) in the $CATALINA_HOME/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. context#path.xml. The default web application may be defined by using a file called ROOT.xml.
  • if the previous file was not found for this application, in an individual file at /META-INF/context.xml inside the application files
  • inside a Host element in the main conf/server.xml

I prefer the /META-INF/context.xml in your project under <web-app>

Put this in your web.xml in your project:

<description>MySQL Test App</description>
 <resource-ref> 
<description>DB Connection</description> 
<res-ref-name>jdbc/TestDB</res-ref-name>       
<res-type>javax.sql.DataSource</res-type> 
<res-auth>Container</res-auth>   
</resource-ref>

***注意:如果你在Netbeans5.5裡面run你的project會有問題,因為netbeans5.5自動include mysql connector,會跟[tomcat 6]\lib裡面的相沖

You are now ready to use Myql in your project.

Prepare JSF

JSF1.2 need Servlet 2.5 and JSP 2.1 which tomcat 6 can provide.

Download JSF and JSTL

Create Web project and Add JSF framework in project properties.

This is the same as doing JSF setting in web.xml, netbeans user don’t need to do this by hand.

<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>
javax.faces.webapp.FacesServlet
</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>

<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

Add JSF managed bean by right clicking in project navigator.

Which will automatically add setting in faces-config.xml by Netbeans,

you can do it yourself by adding the following in faces-config.xml:

<managed-bean>
<managed-bean-name>ManagedBean</managed-bean-name>
<managed-bean-class>maxi.fun.ManagedBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Replace library with those we download previously : jsf-api.jar and jsf-impl.jar for JSF ;standard.jar and jstl.jar for JSTL

JSF file can be .jsf or .jsp, I don’t know how to create .jsf. so I just use .jsp instead.

You are all set. Have Fun

NetBeans Hot Key

NetBeans 常用 Hot-Key:
Ctrl + Shift + F
: Reformat Code (與 eclipse 一樣)
Alt + Shift + F : Fix Imports
Alt + Shift + W : Surround With try-catch
Alt + O : Go to Source
Alt + F7 : Find Usages
Ctrl + F : Find
Ctrl + H : Replace
Ctrl + Shift + T : Comment
Ctrl + Shift + D : Uncomment
Ctrl + \ : Code Completion

Use Your Own Project as Blueprint in Netbeans

From Here

The simplest way is the following :

> Install Netbeans.
> Unjar :
${NB_install_dir}/enterprise[1,2]/modules/org-netbeans-modules-j2ee-blueprints.jar
Note : enterprise[1, 2] is depending on the version of netbeans
> Put custom documents and applications there.
> Edit the two property files for the new projects.
> Edit the two config xml files.
> Create jar.
> Replace the original jar with the new jar.

Very useful.

Not tested yet by my own.

Should be useful.

Develop Portlet (JSR168) with Icefaces

1. Add framework JSF in project properties.

Remember to uncheck library of JSF because we are using Icefaces.

2. In portlet.xml, change <portlet-class> to com.icesoft.faces.webapp.http.portlet.MainPortlet

3. In portlet.xml, add these in

<init-param>
<name>com.icesoft.faces.VIEW</name>
<value>/WEB-INF/jsp/view.iface</value>
</init-param>
<init-param>
<name>com.icesoft.faces.EDIT</name>
<value>/WEB-INF/jsp/edit.iface</value>
</init-param>
<init-param>
<name>com.icesoft.faces.HELP</name>
<value>/WEB-INF/jsp/help.iface</value>
</init-param>

4. In web.xml

The ICEfaces Servlets are registered as follows:

<servlet>
<servlet-name>Persistent Faces Servlet</servlet-name>
<servlet-class>
com.icesoft.faces.webapp.xmlhttp.PersistentFacesServlet
</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>
<servlet>
<servlet-name>Blocking Servlet</servlet-name>
<servlet-class>
com.icesoft.faces.webapp.xmlhttp.BlockingServlet
</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>

The Servlet mappings are established as follows:

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Persistent Faces Servlet</servlet-name>
<url-pattern>*.iface</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Persistent Faces Servlet</servlet-name>
<url-pattern>/xmlhttp/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Blocking Servlet</servlet-name>
<url-pattern>/block/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
com.icesoft.faces.util.event.servlet.ContextEventRepeater
</listener-class>
</listener>
<context-param>
<param-name>com.icesoft.faces.concurrentDOMViews</param-name>
<param-value>true</param-value>
</context-param>

5. Sample .jspx for 1.7 beta

<f:view xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ice="http://www.icesoft.com/icefaces/component">
<html>
<head>
<title>
ICEfaces Portlet Chat
</title>
</head>
<body>
<ice:portlet>
<ice:outputStyle href="/xmlhttp/css/xp/xp-portlet.css" />
<f:loadBundle basename=" " var="bundle"/>
<!-- Login/Logout -->
<ice:form>
<ice:outputText value="ICEFACES 1.7"/>
</ice:form>
</ice:portlet>
</body>
</html>
</f:view>

For 1.6

Remove <ice:portlet>

Put <ice:outputStyle> inside <ice:form>