JSF 2.3+Eclipse+Maven+Tomcat 8入门求助:求pom.xml依赖及配置文件指导
Hey there! Let's get your JSF 2.3 development environment up and running with Eclipse, Maven, and Tomcat 8. I'll break down all the necessary configuration files and setup steps so you can start coding right away.
1. Maven pom.xml Dependencies
First, here's the complete pom.xml with all required dependencies for JSF 2.3, CDI (since Tomcat doesn't include it natively), and Servlet API integration:
<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.yourpackage</groupId> <artifactId>jsf23-tomcat8-starter</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>JSF 2.3 Tomcat 8 Starter</name> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <jsf.version>2.3.14</jsf.version> <weld.version>3.1.9.Final</weld.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- Mojarra: JSF 2.3 Implementation (includes API) --> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.faces</artifactId> <version>${jsf.version}</version> <scope>compile</scope> </dependency> <!-- Weld: CDI Implementation (required for JSF 2.3 features like @Named, @Inject) --> <dependency> <groupId>org.jboss.weld.servlet</groupId> <artifactId>weld-servlet-shaded</artifactId> <version>${weld.version}</version> </dependency> <!-- Servlet API (provided by Tomcat, so we don't package it) --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>jsf23-tomcat8</finalName> <plugins> <!-- Maven WAR Plugin: Disable web.xml requirement (JSF 2.3 supports annotation-based config) --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.2</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
Key Notes:
- Mojarra: This is the official JSF reference implementation—Tomcat doesn't include JSF out of the box, so we need to include this dependency.
- Weld: Tomcat isn't a full Java EE server, so we add Weld to enable CDI (Contexts and Dependency Injection), which is required for many JSF 2.3 features (like managed beans with
@Namedinstead of the old@ManagedBean). - Servlet API: Marked as
providedbecause Tomcat already includes this library.
2. faces-config.xml (WEB-INF/faces-config.xml)
Create this file in your src/main/webapp/WEB-INF directory to configure JSF 2.3 and enable CDI integration:
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd" version="2.3"> <!-- Integrate Weld EL Resolver for CDI support --> <application> <el-resolver>org.jboss.weld.el.WeldELResolver</el-resolver> <!-- Set project stage to Development for better debugging (auto-reload, detailed error messages) --> <project-stage>Development</project-stage> </application> </faces-config>
3. beans.xml (WEB-INF/beans.xml)
This is the CDI configuration file, also placed in src/main/webapp/WEB-INF:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd" version="2.0" bean-discovery-mode="all"> <!-- `bean-discovery-mode="all"` tells CDI to scan all classes for annotations like @Named, @Inject --> <!-- For production, switch to `annotated` to only scan classes with explicit CDI annotations --> </beans>
4. Eclipse & Tomcat Setup Tips
Once your config files are set, here's how to get things running in Eclipse:
- Import the Maven Project: Go to
File > Import > Maven > Existing Maven Projects, select your project directory, and finish the import. - Configure Tomcat 8: Head to
Window > Preferences > Server > Runtime Environments, clickAdd, selectApache Tomcat v8.0, and browse to your Tomcat installation folder. - Add Project to Tomcat: In the
Serversview (if you don't see it, enable it viaWindow > Show View > Servers), right-click your Tomcat server, chooseAdd and Remove, select your project, and clickFinish. - Verify Java Version: Make sure your project uses Java 8 (Tomcat 8's supported version): Right-click project >
Properties > Java Build Path > Libraries, check thatJRE System Libraryis set toJava SE 1.8. - Test the App: Start the Tomcat server, then visit
http://localhost:8080/jsf23-tomcat8/(replacejsf23-tomcat8with yourfinalNamefrom pom.xml). You can add a simpleindex.xhtmlfile insrc/main/webappto test rendering.
内容的提问来源于stack exchange,提问作者kArThIk sWaMiNaThAn
相关产品推荐
相关产品推荐

