JSFFlows、数据填充与安全防护技术解析
立即解锁
发布时间: 2025-08-18 00:05:50 阅读量: 1 订阅数: 3 

### JSF Flows、数据填充与安全防护技术解析
#### 1. JSF Flows的运用
在JSF开发中,为了使页面内容设计更加统一,我们可以通过特定方式从作用域中获取实体并将其设置为Bean属性。具体操作如下:
- 对`FootprintFlow`类使用`@PostConstruct`注解。
- 在`initialize()`方法中,通过`FacesContext`实例以编程方式从流作用域中获取对象。需要注意的是,参数名`param3Value`必须与XML定义中的值一致,此时流作用域中的值恰好是`CarbonFootprint`实体。
以下是页面视图`sector-flow/sector-flow.xhtml`的部分代码示例:
```xml
<h1>Page <code>sector-flow.xhtml</code></h1>
<table class="table table-bordered table-striped">
<tr>
<th>Expression</th>
<th>Value</th>
</tr>
<tr>
<td>sectorFlow.footprint.applicationId</td>
<td>#{sectorFlow.footprint.applicationId}</td>
</tr>
<tr>
<td>sectorFlow.footprint</td>
<td>#{sectorFlow.footprint}</td>
</tr>
</table>
<h:form prependId="false">
<div class="form-group">
<label jsf:for="exampleInputEmail1">
Email address</label>
<input type="email" class="form-control"
jsf:id="exampleInputEmail1" placeholder="Enter email"
jsf:value="#{flowScope.email}"/>
</div>
<div class="form-group">
<h:outputLabel for="industryOrSector">Industry or Sector</h:outputLabel>
<h:inputText type="text" class="form-control"
id="industryOrSector"
placeholder="Your industry sector"
value="#{sectorFlow.footprint.industryOrSector}"/>
</div>
<h:commandButton id="nextBtn" styleClass="btn btn-primary btn-lg"
value="Next" action="sector-flow-1a" />
<h:commandButton id="exitFlowBtn" styleClass="btn btn-primary btn-lg"
value="Exit Flow" action="endFlow" />
<h:commandButton id="homeBtn" styleClass="btn btn-primary btn-lg"
value="Home" action="goHome" />
<h:commandButton id="callFootPrintFlowBtn" styleClass="btn btn-primary btn-lg"
value="Call Footprint" action="callFootprintFlow" />
<h:commandButton id="saveBtn" styleClass="btn btn-primary btn-lg"
value="Save Record" action="#{sectorFlow.saveFootprintRecord()}" />
</h:form>
```
该视图混合使用了HTML5友好标记和标准JSF标签。例如,输入文本字段`exampleInputEmail1`及其关联标签体现了HTML5友好标记,此输入控件与流作用域映射集合`#{flowScope.email}`关联;输入元素`industryOrSector`展示了JSF标准标签,它直接与`CarbonFootprint`实体记录关联。各按钮功能如下:
| 按钮ID | 功能 |
| ---- | ---- |
| nextBtn | 直接导航到流中的下一个视图 |
| exitFlowBtn | 退出流 |
| homeBtn | 退出流并返回主页 |
| callFootPrintFlowBtn | 调用嵌套流,其操作`callFootprintFlow`与`sector-flow.xml`文件中的节点完全匹配 |
| saveBtn | 调用支持Bean中的`saveFootprintRecord()`方法 |
#### 2. 应用数据填充
在开发应用时,为了确保应用能正确填充数据库,我们可以采用以下方法:
- 创建一个`DataPopulator`类,使用`@Singleton`和`@Startup`注解。`@Startup`注解确保EJB容器在应用部署后初始化该Bean,`@Singleton`注解表示会话Bean在应用中最多只有一个实例。
```java
package uk.co.xenonique.digital.product.utils;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
@Singleton
@Startup
public class DataPopulator {
@Inject ExtendedPersistenceLoaderBean loaderBean;
@PostConstruct
public void populate() {
loaderBean.loadData();
}
}
```
- `ExtendedPersistenceLoaderBean`是一个具有新CDI 1.1事务作用域的CDI Bean,用于实际的数据加载操作。
```java
package uk.co.xenonique.digital.product.utils;
import uk.co.xenonique.digital.product.boundary.*;
import uk.co.xenonique.digital.product.entity.*;
import javax.annotation.*;
import javax.ejb.*;
import javax.inject.Inject;
import java.io.Serializable;
import java.util.*;
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@javax.transaction.TransactionScoped
public class ExtendedPersistenceLoaderBean implements Serializable {
public static final String DEFAULT = "digital";
@Inject
UserProfileService service;
@PostConstruct
public void init() { /* ... */ }
@PreDestroy
public void destroy() { /* ... */ }
public void loadData() {
UserRole userRole = new UserRole("user");
UserRole managerRole = new UserRole("manager");
List<UserProfile> users = Arrays.asList(
new UserProfile("[email protected]", DEFAULT, userRole),
new UserProfile("[email protected]", DEFAULT, userRole),
new UserProfile("[email protected]", DEFAULT, managerRole)
```
0
0
复制全文
相关推荐









