搜索引擎是软件还是网站,wordpress 树形页面,网页版qq安全中心登录入口,湛江在线制作网站在上一篇jsf环境搭建的基础上 , 加入spring框架 , 先看下目录结构 src/main/resources 这个source folder 放置web项目所需的主要配置,打包时,会自动打包到WEB-INF下 首先看下pom.xml,需要引入一些依赖项: 1 project xmlnshttp://maven.apache.org/POM/4.0.0 x…在上一篇jsf环境搭建的基础上 , 加入spring框架 , 先看下目录结构 src/main/resources 这个source folder 放置web项目所需的主要配置,打包时,会自动打包到WEB-INF下 首先看下pom.xml,需要引入一些依赖项: 1 project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance2 xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd3 modelVersion4.0.0/modelVersion4 groupIdyjmyzz/groupId5 artifactIdjsf-web/artifactId6 version1.0/version7 packagingwar/packaging8 9
10 dependencies
11 !-- 单元测试 --
12 dependency
13 groupIdjunit/groupId
14 artifactIdjunit/artifactId
15 version4.7/version
16 scopetest/scope
17 /dependency
18
19 !-- jsf --
20 dependency
21 groupIdorg.jboss.spec.javax.faces/groupId
22 artifactIdjboss-jsf-api_2.1_spec/artifactId
23 version2.1.19.1.Final-redhat-1/version
24 scopecompile/scope
25 /dependency
26
27
28 !-- spring --
29 dependency
30 groupIdorg.springframework/groupId
31 artifactIdspring-context/artifactId
32 version4.0.2.RELEASE/version
33 /dependency
34
35
36 dependency
37 groupIdorg.springframework/groupId
38 artifactIdspring-web/artifactId
39 version4.0.2.RELEASE/version
40 /dependency
41
42
43 !-- servlet支持 --
44 dependency
45 groupIdjavax.servlet/groupId
46 artifactIdservlet-api/artifactId
47 version2.5/version
48 /dependency
49
50 /dependencies
51
52 build
53 plugins
54 plugin
55 artifactIdmaven-compiler-plugin/artifactId
56 version3.1/version
57 configuration
58 source1.7/source
59 target1.7/target
60 /configuration
61 /plugin
62 plugin
63 artifactIdmaven-war-plugin/artifactId
64 version2.3/version
65 configuration
66 warSourceDirectorywebapp/warSourceDirectory
67 failOnMissingWebXmlfalse/failOnMissingWebXml
68 /configuration
69 /plugin
70 /plugins
71 /build
72 /project pom.xml 1. 自动加载配置文件 在web项目中,可以让spring自动加载配置文件(即上图中的src/main/resouces/spring下的xml文件),WEB-INF/web.xml中参考以下设置: 1 ?xml version1.0 encodingUTF-8?2 web-app xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlnshttp://java.sun.com/xml/ns/javaee xsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd idWebApp_ID version3.03 display-namejsf-web/display-name4 5 welcome-file-list6 welcome-fileindex.html/welcome-file 7 /welcome-file-list8 9 listener
10 listener-class
11 org.springframework.web.context.ContextLoaderListener
12 /listener-class
13 /listener
14
15 context-param
16 param-namecontextConfigLocation/param-name
17 param-value
18 classpath*:spring/applicationContext-*.xml
19 /param-value
20 /context-param
21
22 /web-app web.xml 解释一下: classpath*:spring/applicationContext-*.xml 这里表示将加载classpath路径下 spring目录下的所有以applicationContext-开头的xml文件 , 通常为了保持配置文件的清爽 , 我们会把配置分成多份 : 比如 applicationContext-db.xml 用来配置DataSource , applicationContext-cache.xml用来配置缓存...等等. 2.代码中如何取得ApplicationContext实例 1 package yjmyzz.utils;2 3 import javax.faces.context.FacesContext;4 import javax.servlet.ServletContext;5 6 import org.springframework.context.ApplicationContext;7 import org.springframework.web.context.support.WebApplicationContextUtils;8 9 public class ApplicationContextUtils {
10
11 public static ApplicationContext getApplicationContext() {
12 ServletContext context (ServletContext) FacesContext
13 .getCurrentInstance().getExternalContext().getContext();
14 ApplicationContext appctx WebApplicationContextUtils
15 .getRequiredWebApplicationContext(context);
16
17 return appctx;
18 }
19
20 public static T T getBean(ClassT t) {
21 return getApplicationContext().getBean(t);
22 }
23 } ApplicationContextUtils 有了这个工具类 , 就可以方便的取得注入的Bean 3. 使用properties文件注入 为了演示注入效果,先定义一个基本的Entity类 1 package yjmyzz.entity;2 3 import java.io.Serializable;4 5 public class ProductEntity implements Serializable {6 7 private static final long serialVersionUID -2055674628624266800L;8 /*9 * 产品编码
10 */
11 private String productNo;
12
13 /**
14 * 产品名称
15 */
16 private String productName;
17
18 /**
19 * 产品ID
20 */
21 private Long productId;
22
23 public String getProductNo() {
24 return productNo;
25 }
26
27 public void setProductNo(String productNo) {
28 this.productNo productNo;
29 }
30
31 public String getProductName() {
32 return productName;
33 }
34
35 public void setProductName(String productName) {
36 this.productName productName;
37 }
38
39 public Long getProductId() {
40 return productId;
41 }
42
43 public void setProductId(Long productIdLong) {
44 this.productId productIdLong;
45 }
46
47 Override
48 public String toString() {
49 return productId / productNo / productName;
50 }
51
52 } ProductEntity 然后在applicationContext-beans.xml中配置以下内容: 1 ?xml version1.0 encodingUTF-8?2 beans xmlnshttp://www.springframework.org/schema/beans3 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance4 xsi:schemaLocationhttp://www.springframework.org/schema/beans5 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd6 7 bean idpropertyConfigurer8 classorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer9 property namelocations
10 list
11 valueclasspath:properties/*.properties/value
12 /list
13 /property
14 /bean
15
16 bean idproductEntity classyjmyzz.entity.ProductEntity
17 property nameproductId value${product.id} /
18 property nameproductNo value${product.no} /
19 property nameproductName value${product.name} /
20 !-- property nameproductId
21 bean classjava.lang.Long
22 constructor-arg index0 value${product.id} /
23 /bean
24 /property
25 --
26 /bean
27 /beans spring配置文件 注:classpath:properties/*.properties表示运行时 , spring容器会自动加载classpath\properties目录下的所有以.properties后缀结尾的文件 , 我们在src/main/resources/properties/下放置一个product.properties属性文件 , 内容如下: 1 product.id3
2 product.non95
3 product.namephone product.properties 该文件被spring自动加载后 , 就可以用里面定义的属性值 , 为Bean做setter属性注入 , 即配置文件中的property nameproductId value${product.id} / 4.验证注入是否成功 在HomeController里 , 向Spring容器要一个Bean , 显示下它的属性: 1 package yjmyzz.controller;2 3 import javax.faces.bean.ManagedBean;4 5 import yjmyzz.entity.ProductEntity;6 import yjmyzz.utils.ApplicationContextUtils;7 8 ManagedBean(name Home)9 public class HomeController {
10
11 public String sayHello() {
12
13 ProductEntity product ApplicationContextUtils
14 .getBean(ProductEntity.class);
15
16 return product.toString();
17 }
18
19 } HomeController index.xhtml里仍然跟上篇相同: 1 !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd 2 html xmlnshttp://www.w3.org/1999/xhtml3 xmlns:hhttp://java.sun.com/jsf/html4 xmlns:fhttp://java.sun.com/jsf/core5 xmlns:uihttp://java.sun.com/jsf/facelets 6 7 h:head8 titlejsf-web/title9 /h:head
10 body
11 h1
12 #{Home.sayHello()}
13
14 /h1
15 /body
16 /html index.xhtml 最后部署到jboss上 , 运行截图如下: 转载于:https://www.cnblogs.com/yjmyzz/p/3604007.html