W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
下面的代碼顯示了如何使用Spring來使用JSF。
以下代碼來自PrinterImpl.java。
package cn.w3cschool.common; import javax.inject.Named; @Named public class PrinterImpl implements Printer{ public String print() { return "JSF 2 + Spring Integration"; } }
下面的代碼來自Printer.java。
package cn.w3cschool.common; public interface Printer{ public String print(); }
以下代碼來自default.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:h="http://java.sun.com/jsf/html"> <h:body>#{userBean.printMsgFromSpring()}</h:body> </html>
下面的代碼來自UserBean.java。
package cn.w3cschool.common; import javax.inject.Inject; import javax.inject.Named; import org.springframework.context.annotation.Scope; @Named @Scope("session") public class UserBean { @Inject Printer printer; public void setPrinter(Printer p) { this.printer = p; } public String printMsgFromSpring() { return printer.print(); } }下載 JSF_Spring.zip
<?xml version="1.0" encoding="UTF-8"?> <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>com.w3cschool.jsf</groupId> <version>1.0-SNAPSHOT</version> <artifactId>simple-webapp</artifactId> <packaging>war</packaging> <name>Simple Web Application</name> <dependencies> <!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.1.2.RELEASE</version> </dependency> <!-- JSF --> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.1.13</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.1.13</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> </dependency> <!-- EL --> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>el-impl</artifactId> <version>2.2</version> </dependency> <!-- Tomcat 6 need this --> <dependency> <groupId>com.sun.el</groupId> <artifactId>el-ri</artifactId> <version>1.0</version> </dependency> <!-- JSR-330 --> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> </dependencies> <build> <finalName>simple-webapp</finalName> </build> </project>
以下代碼來自applicationContext.xml。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:component-scan base-package="com.w3cschool" /> </beans>
將生成的WAR文件從目標(biāo)文件夾復(fù)制到Tomcat部署文件夾,并運行Tomcat-Install-folder/bin/startup.bat。
Tomcat完成啟動后,在瀏覽器地址欄中鍵入以下URL。
http://localhost:8080/simple-webapp/default.xhtml
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: