![](/img/trans.png)
[英]How to pass class constructor parameters in Spring bean Autowired by annotations
[英]How to pass parameters via URL to a bean class using JSF?
我已經為我的JSF應用程序創建了一個登錄頁面。 我想通過URL將用戶名和密碼作為參數傳遞,以便稍后將它們作為bean類中的字段接收。 我怎樣才能做到這一點?
您應該將其作為POST參數傳遞,這是JSF默認執行的操作。您可以使用google快速查看帶有JSF的Login頁面,但是如果您想從URL讀取請求參數,那么您可以這樣做
<a href="name.jsf?id=#{testBean.id}" />
你的bean需要這樣的東西
@ManagedBean
@RequestScoped
public class TestBean {
@ManagedProperty(value = "#{param.id}")
private String id;
.....
}
您也可以在xhtml中執行此操作以獲得相同的結果,這將適用於JSF 2.x,因為JSPa 1.2中沒有viewParam
<f:metadata>
<f:viewParam name="id" value="#{testBean.id}" />
</f:metadata>
上面的行將在創建bean時從請求參數id設置bean中的參數id。
首先,如果您考慮將用戶名和密碼作為查詢字符串的一部分附加。 然后,不要這樣做,你使你的系統易受攻擊。
關於你的問題的答案:
<h:commandLink action="#{ttt.goToViewPage()}" value="View">
<!-- To access via f:param tag, this does not maps directly to bean. Hence using context fetch the request parameter from request map. -->
<!-- <f:param name="selectedProfileToView" value="#{profile.id}" /> -->
<!-- Using this to replace the f:param tag to avoid getting the request object -->
<f:setPropertyActionListener target="#{ttt.selectedStudentProfile}" value="#{profile.id}" />
</h:commandLink>
f:param (如注釋中所述),這不會直接映射到bean屬性,但是您必須使用context來獲取請求對象,您可以從requestparametermap引用該值。
FacesContext context = FacesContext.getCurrentInstance();
Map<String, String> requestMap = context.getExternalContext().getRequestParameterMap();
f:setPropertyActionListener ,這是另一個屬性,它將直接映射到托管bean的屬性。
<h:commandLink action="#{ttt.goToEditPage(profile.id)}" value="Edit">
如果你看這里我已經提到了函數中的參數。 托管bean類中應該存在具有類似簽名的方法,該值將直接映射到函數參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.