一个简单的JSF 2.0例子
开发环境:
JDK 1.6.0
Tomcat 6.0.18
Eclipse 3.4.1
Maven 2.1.0
m2eclipse 0.9.7
JSF 2.0.0
首先使用m2eclipse创建一个简单的maven项目。然后添加FacesServlet到web.xml中:
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
接着,就是把JSF2的jar包加入pom.xml文件中:
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.0-b09</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.0.0-b09</version>
<scope>compile</scope>
</dependency>
</dependencies>
再创建一个managed bean:
@ManagedBean(name="mainbean")
public class MainBean {
public String getText() {
return "Hello, JSF 2.0";
}
}
还要创建一个视图:
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>A simple JSF 2.0 example</title>
</h:head>
<h:body>
<h:outputText value="#{mainbean.text}" />
</h:body>
</html>
就这样,一个简单的JSF2例子就完成了。
看完这个例子后你可能会注意到上面并没有创建faces-config.xml这个文件。是的,这是因为这个例子实在是太简单了,以至于不需要faces-config.xml也可以:)
Related Posts
[SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property...
使用Tomcat 6.0.16搭配Eclipse创建Dynamic Web Project后,在项目上按右键,选择Run As -> Run on Server方式启动Tomcat服务器。在Console工作台中就会出现如下的红色警告信息,让人不爽。
2008-6-4 21:49:00 org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:cont' did not find a matching property.
2008-6-4 21:49:00 org.apache.catalina.core.AprLifecycleListener init
2008-6-4 21:49:00 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
在网上查找答案,有的说将Tomcat的版本换成6.0.14就可以了;还有的说在建立Dynamic Web Project时把"Dynamic Web Module"选项后面的版本号由2.5改成2.4也行。不过这些回答好像都不能真正地解决问题。只能自己想办法查找解决方法。
实际上出现SetPropertiesRule警告的原因是因为Tomcat在server.xml的Context节点中不支持source属性:
<Context docBase="..." path="/cont" source="org.eclipse.jst.j2ee.server:cont"/>
解决方法是在Server的配置界面中选中"Publish module contexts to separate XML files"选项。
Related Posts
在Eclipse-3.x.x 中使用ssh2 来访问gro.clinux.org 的cvs 服务器
1、在Eclipse-3.x.x 中生成公钥和私钥
打开Eclipsev-3.x.x,进入Window > Preferences > Team > CVS > SSH2 Connection Method > Key Management,点击Generate RSA Key... 然后填写Passphrase 和Confirm Passphrase,
接着点击Save Private Key... 保存公钥和私钥(公钥和私钥文件保存在C:Documents and Settingsusernamessh 下,id_rsa 是私钥文件,id_rsa.pub 是公钥文件)。
2、将公钥上传至gro.clinux.org 服务器
登陆gro 服务器,进入[帐号维护],在页面底部你可以看到如下信息:
Shell帐号信息
Shell box:
CVS/SSH 共享认证Key:0 [编辑Keys]
点击[Edit Keys],然后把刚才生成的文件id_rsa.pub 中的内容粘贴到文本框中(看看是不是符合它上面的要求,千万要仔细看看),如果没有问题的话,就更新吧。
大约六个小时后cron 会自动更新,在服务器上你的目录下会自动添加目录/.ssh/,目录里包含了名为authorized_keys2 的文件,你的公钥就保存在这里。
3、在Eclipse-3.x.x 中建立CVS 连接
进入CVS Repository Exploring Perspective,进入New > Repository Location,填入以下内容:
Host:cvs.unixname.gro.clinux.org
Repository Path:/cvsroot/unixname
User:你在gro.clinux.org 上注册的帐户
Password:先前保存公钥和私钥时输入的Passphrase
Connection Type:extssh
点击Finish。如果你是第一次登录的话,将会弹出一个对话框,不用看那么仔细,直接点击OK 就好了。
Related Posts
People who read this, also read...
Eclipse环境中Junit结合Ant进行自动测试的问题(2)
Environment:eclipse-3.0.3 + ant-1.6.2 + junit-3.8.1
// build.xml
<target name="test">
<junit>
<formatter type="xml" />
<batchtest todir="${report.dir}">
<fileset dir="${classes.dir}" includes="**/*Test.class" />
</batchtest>
</junit>
</target>
执行脚本的时候出现了以下的错误:
java.lang.ClassNotFoundException: example.AdditionTest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
将<fileset dir="${classes.dir}" includes="**/*Test.class" /> 改成<...includes="**/*Test.*" />
或者<...includes="**/*Test" /> 都不行。只有改成<...includes="**/*Test.java" />才可以,可
是改成<...includes="**/*Test.java" /> 后测试无任何效果,肯定通过。用google 搜也搜
不到答案,到ant 网站http://ant.apache.org/faq.html#delegating-classloader上去查找,才
找到原因。
在上述情况中,Ant 不会去加载example.AdditionTest。而只是加载了junit库,junit
再去加载example.AdditionTest,由于java 加载采用的是delegation 模式,而且junit 和
example.AdditionTest 不在同一个空间,所以junit 会调用parent 的classLoader 来加载
库,结果就出现上面的ClassNotFoundException 提示信息啦。
因此,只要载<junit></junit> 间加入classpath 指出要加载库的路径就可以了,修改
后的build.xml 文件如下:
// build.xml
<target name="test">
<junit>
<classpath>
<pathelement location="${classes.dir}" />
</classpath>
<formatter type="xml" />
<batchtest todir="${report.dir}">
<fileset dir="${classes.dir}" includes="**/*Test.class" />
</batchtest>
</junit>
</target>
Related Posts
People who read this, also read...
Eclipse环境中Junit结合Ant进行自动测试的问题(1)
Eclipse 3.0.3
Ant 1.6.2
Junit 3.8.1
build.xml
<target name="test">
<junit>
...
</junit>
</target>
在执行时出现了以下问题:
BUILD FAILED: C:eclipse-3.0.3workspaceDailyBuildbuild.xml:44: Could not create task or type of type: junit.
Ant could not find the task or a class this task relies upon.
This is common and has a number of causes; the usual
solutions are to read the manual pages then download and
install needed JAR files, or fix the build file:
- You have misspelt 'junit'.
Fix: check your spelling.
- The task needs an external JAR file to execute
and this is not found at the right place in the classpath.
Fix: check the documentation for dependencies.
Fix: declare the task.
- The task is an Ant optional task and the JAR file and/or libraries
implementing the functionality were not found at the time you
yourself built your installation of Ant from the Ant sources.
Fix: Look in the ANT_HOME/lib for the 'ant-' JAR corresponding to the
task and make sure it contains more than merely a META-INF/MANIFEST.MF.
If all it contains is the manifest, then rebuild Ant with the needed
libraries present in ${ant.home}/lib/optional/ , or alternatively,
download a pre-built release version from apache.org
- The build file was written for a later version of Ant
Fix: upgrade to at least the latest release version of Ant
- The task is not an Ant core or optional task
and needs to be declared using <taskdef>.
- You are attempting to use a task defined using
<presetdef> or <macrodef> but have spelt wrong or not
defined it at the point of use
Remember that for JAR files to be visible to Ant tasks implemented
in ANT_HOME/lib, the files must be in the same directory or on the
classpath
Please neither file bug reports on this problem, nor email the
Ant mailing lists, until all of these causes have been explored,
as this is not an Ant bug.
产生上面的错误原因是Ant在执行Junit程序时没有找到junit.jar这个文件,将junit.jar文件copy到ANT_HOME/lib目录下,再次执行ant test任务,waiting...
这下该OK了吧^_^
有没有搞错,还产生同样的错误!到窗口下试试。进入命令行窗口,输入ant test,再次等待ing
Buildfile: build.xml test: BUILD SUCCESSFUL Total time: 1 second
怎么会这样的???命令行窗口下可以,Eclipse下就不行了。
到Preferences -> Ant -> Runtime -> Classpath下设置一下junit.jar的路径,再次执行ant test任务,终于在console中看到了
test: BUILD SUCCESSFUL Total time: 4 seconds
Related Posts
People who read this, also read...
在Eclipse下调试Servlet时出现Source not found for......后
在Eclipse下调试Servlet出现了Source not found for XxxAction.execute (...)的问题,猜想是没有将Servlet代码加入到debug路径下以致在调试的时候找不到Source,虽然知道原因,却不知道怎么解决。只能在Eclipse里乱逛,东点西击,最后终于发现了问题所在。
进入Window -> Preferences -> Tomcat -> Source Path,在Source Path标签下有行文字:
Add java projects to source path (Eclipse debugger will search for source files in these projects)。
将要调试的project选中然后再调试,果然不再出现Source not found for...了!!

