我試着去了解一個JSF示例出書的Java EE7發展與Wildfly的,但這裏是我不明白 - 即使它的工作原理:@Produces在成員變量
豆:
@Named
@RequestScoped
public class TheatreSetupService {
...
@Produces
@Named
private SeatType newSeatType;
@PostConstruct
public void initNewSeatType() {
newSeatType = new SeatType();
}
....
}
XHTML:
<h:form id="reg" role="form">
<div class="form-group has-feedback #{!desc.valid? 'has-error' : ''}">
<h:outputLabel for="desc" value="Description"
styleClass="control-label"/>
<h:inputText id="desc" value="#{newSeatType.description}"
p:placeholder="Enter a description here" class="form-control"
binding="#{desc}"/>
<span class="#{!desc.valid ? 'glyphicon glyphicon-remove form-control-feedback' : ''}"/>
<h:message for="desc" errorClass="control-label has-error"/>
</div>
</h:form>
實體:
@Entity
@Table(name = "seat_type")
public class SeatType implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Size(min = 1, max = 25, message = "Enter a Seat Description (max 25 char)")
@Pattern(regexp = "[A-Za-z ]*", message = "Description must contain only letters and spaces")
private String description;
private SeatPosition position;
@NotNull
private Integer price;
@NotNull
private Integer quantity;
@OneToMany(mappedBy = "seatType", fetch = FetchType.EAGER)
private List<Seat> seats;
public SeatType() {
// empty for jpa
}
...
}
我不明白@Produces對成員變量newSeatType的影響。創建由TheatreSetupService類明確管理。對我來說,它看起來就像是一個導出,該成員可用於jsf,但@Named註釋不足以使此示例正常工作。任何人都可以解釋我在這個小例子中發生了什麼?據我看,這不是很常用 - 這是真的嗎?
謝謝你的提示!
Dominic