2013年5月12日 星期日

Maven教學 - 使用Maven建立 Jave web + Jsf2 專案 ( Tomcat7 )

1.使用Maven + Eclipse建立Java Web專案

2.為了使用Jsf2,需要在web.xml中設定

web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <display-name>Jsf2 Demo</display-name>

 <!-- Change to "Production" when you are ready to deploy -->
 <context-param>
  <param-name>javax.faces.PROJECT_STAGE</param-name>
  <param-value>Development</param-value>
 </context-param>

 <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>*.xhtml</url-pattern>
 </servlet-mapping>

 <!-- Welcome page -->
 <welcome-file-list>
  <welcome-file>helloJsf2.xhtml</welcome-file>
 </welcome-file-list>

</web-app>
 註:
本文使用的server是tomcat 7 ,不同server版本可能需要加入不同套件


2.所需要的套件(jar檔)透過maven的設定取得

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Demo</groupId>
  <artifactId>Jsf2</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Jsf2 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
  
         <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.7</version>
        </dependency>
        
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.7</version>
        </dependency>
  
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>Jsf2</finalName>
  </build>
</project>

3.建立Demo頁面與對應的ManagedBean


 helloJsf2.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://java.sun.com/jsf/core"
 xmlns:h="http://java.sun.com/jsf/html">
<h:head>
 <title>JSF 2.0 Hello World</title>
</h:head>
<h:body bgcolor="white">
 <h3>JSF 2.0 Demo </h3>
 <h:form>
  <h:outputLabel value="input :" />
  <h:inputText value="#{helloJsf2Bean.msg}" />
  <h:commandButton value="submit" /><br/>
  <h:outputLabel value="msg : #{helloJsf2Bean.msg}" />
 </h:form>

</h:body>
</html>
 HelloJsf2Bean.java
package mkn939.demo.mbean;

import javax.faces.bean.ManagedBean;
 
@ManagedBean
public class HelloJsf2Bean {
 String msg;

 public String getMsg() { 
  return msg;
 }

 public void setMsg(String msg) {
  this.msg = msg;
 }

}

實際Demo畫面

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...