Jsf 如何使用链接调用bean操作方法?onclick不起作用
我正在尝试实现一个用户名列表,可以通过单击向上或向下链接来重新排列Jsf 如何使用链接调用bean操作方法?onclick不起作用,jsf,jsf-2,hyperlink,onclick,actionmethod,Jsf,Jsf 2,Hyperlink,Onclick,Actionmethod,我正在尝试实现一个用户名列表,可以通过单击向上或向下链接来重新排列 命令链接和托管bean之间的通信正在工作,但在UI中只显示最后一个命令链接(关闭操作)。使用onclick属性调用JavaScript函数(客户端)。当您想要附加JavaScript单击事件handler时,可以使用它 “#{accountController.moveDown}”是一个方法表达式。顾名思义,accountController是一个托管bean 正如政府所说: javax.el.ValueExpression(必
命令链接和托管bean之间的通信正在工作,但在UI中只显示最后一个命令链接(关闭操作)。使用
onclick
属性调用JavaScript函数(客户端)。当您想要附加JavaScript单击事件handler时,可以使用它
“#{accountController.moveDown}”
是一个方法表达式。顾名思义,accountController
是一个托管bean
正如政府所说:
javax.el.ValueExpression(必须计算为java.lang.String)
可以是最终必须计算为字符串的值表达式
在该元素上单击指针按钮时执行的Javascript代码
更新:
可能就是你要找的。您可以使用
action
属性来调用backingbean方法。为了在单击链接时调用bean操作方法,您需要
。这必须包含在
中
在JSF中,只有将EL表达式解释为的方法才能用于声明动作方法。当JSF生成HTML输出时,所有其他属性都被解释为并立即执行。这包括onclick
属性,其值实际上应该表示一个JavaScript函数
如果您确实想使用GET链接,请将操作方法移动到目标页面中的
。这将在目标页面的页面加载时调用
<h:link ... outcome="/other.xhtml" />
另见:
按照你的建议,我把所有的链接都放在一个表格里 命令链接和托管bean之间的通信正在工作,但在UI中只显示最后一个commandLink(关闭操作) 您不应该将多个物理上不同的组件绑定到同一个bean属性。另外,传递参数的
在JSF2中已经不需要了。假设您使用的是Servlet 3.0/EL 2.2容器(您的问题历史记录确认您使用的是Glassfish 3),而不是直接将参数作为方法参数传递:
<h:commandLink id="Link1" value="Up" action="#{accountController.moveUserUp(user)}" />
<h:commandLink id="Link2" value="Down" action="#{accountController.moveUserDown(user)}" />
<h:commandLink id="Link3" value="Delete" action="#{accountController.deleteUser(user)}" />
另见:
我已经修改了您的代码,如果这是您正在查看的内容,请告诉我
托管bean(ManageUser)
导入java.io.Serializable;
导入java.util.ArrayList;
导入java.util.List;
导入javax.faces.bean.ManagedBean;
导入javax.faces.bean.ViewScoped;
@ManagedBean(name=“manageUser”)
@视域
公共类ManageUser实现了可序列化{
/**
*
*/
私有静态最终长serialVersionUID=-53387641550232424249L;
私有列表用户列表;
私有用户bean用户;
/**
*@返回用户
*/
公共UserBean getUser(){
返回用户;
}
/**
*@param user要设置的用户
*/
公共void setUser(UserBean用户){
this.user=用户;
}
/**
*@返回用户列表
*/
公共列表getUserList(){
返回用户列表;
}
/**
*@param userList要设置的用户列表
*/
public void setUserList(List userList){
this.userList=userList;
}
公共ManageUser(){
UserBean user1=新的UserBean();
user1.setUserId(“1”);
user1.setUserName(“userName1”);
UserBean user2=新的UserBean();
user2.setUserId(“2”);
user2.setUserName(“userName2”);
UserBean user3=新的UserBean();
user3.setUserId(“3”);
user3.setUserName(“userName3”);
userList=newarraylist();
userList.add(user1);
userList.add(user2);
userList.add(user3);
}
公共void moveUserDown(){
如果(用户!=null){
int indexObj=userList.indexOf(用户);
if(indexObj0){
UserBean tempUser=userList.get(indexObj-1);
userList.set(indexObj-1,用户);
set(indexObj,tempUser);
}
}
}
}
用户bean
导入java.io.Serializable;
公共类UserBean实现了可序列化{
/**
*
*/
私有静态最终长serialVersionUID=3820279264217591645L;
私有字符串用户名;
私有字符串用户标识;
/**
*@返回用户名
*/
公共字符串getUserName(){
返回用户名;
}
/**
*@param userName要设置的用户名
*/
public void setUserName(字符串用户名){
this.userName=用户名;
}
/**
*@返回用户ID
*/
公共字符串getUserId(){
返回用户标识;
}
/**
*@param userId要设置的用户标识
*/
public void setUserId(字符串userId){
this.userId=userId;
}
}
那么有没有办法以同样的方式访问支持bean方法呢?h:commandLink有action等属性,actionListener可以用来调用managedbean方法。但是对于command Link,我还必须创建一个表单。没有其他解决办法吗?某种类型的事件管理?首先,调用managedbea需要表单(帮助JSF传递要调用的方法的信息)
private UIComponent commandLink;
public void moveUserUp(){
Integer userId = (Integer)commandLink.getAttributes().get("userId");
System.out.println("MOVE TAB LEFT :" + userId);
}
public void moveUserDown(){
Integer userId = (Integer)commandLink.getAttributes().get("userId");
System.out.println("MOVE TAB RIGHT: " + userId);
}
public void deleteUser(){
Integer userId = (Integer)commandLink.getAttributes().get("userId");
System.out.println("DELETE TAB: " + userId);
}
public UIComponent getCommandLink() {
return commandLink;
}
public void setCommandLink(UIComponent commandLink) {
this.commandLink = commandLink;
}
<h:form>
<h:commandLink ... action="#{bean.action}" />
</h:form>
public String action() {
// ...
return "/other.xhtml";
}
<h:link ... outcome="/other.xhtml" />
<f:metadata>
<f:viewAction action="#{bean.onload}" />
</f:metadata>
public void onload() {
// ...
}
<h:commandLink id="Link1" value="Up" action="#{accountController.moveUserUp(user)}" />
<h:commandLink id="Link2" value="Down" action="#{accountController.moveUserDown(user)}" />
<h:commandLink id="Link3" value="Delete" action="#{accountController.deleteUser(user)}" />
public void moveUserUp(User user) {
// ...
}
public void moveUserDown(User user) {
// ...
}
public void deleteUser(User user) {
// ...
}
I have modified your code, let me know if this is what you are looking at achive
<h:form>
<a4j:outputPanel id="userList" ajaxRendered="false">
<ui:repeat value="#{manageUser.userList}" var="user">
<div class="user">
<h:panelGrid columns="3">
<h:outputText value="#{user.userId} ---- #{user.userName} ---- " />
<a4j:commandLink id="LinkUp" value="Up" execute="@this"
action="#{manageUser.moveUserUp}" limitRender="true" render="userList" >
<f:setPropertyActionListener value="#{user}" target="#{manageUser.user}" />
</a4j:commandLink>
<a4j:commandLink id="LinkDown" value="down"
action="#{manageUser.moveUserDown}" execute="@this" limitRender="true" render="userList" >
<f:setPropertyActionListener value="#{user}" target="#{manageUser.user}" />
</a4j:commandLink>
</h:panelGrid>
</div>
</ui:repeat>
</a4j:outputPanel>
</h:form>
Managed Beans (ManageUser)
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean(name="manageUser")
@ViewScoped
public class ManageUser implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5338764155023244249L;
private List<UserBean> userList;
private UserBean user;
/**
* @return the user
*/
public UserBean getUser() {
return user;
}
/**
* @param user the user to set
*/
public void setUser(UserBean user) {
this.user = user;
}
/**
* @return the userList
*/
public List<UserBean> getUserList() {
return userList;
}
/**
* @param userList the userList to set
*/
public void setUserList(List<UserBean> userList) {
this.userList = userList;
}
public ManageUser() {
UserBean user1= new UserBean();
user1.setUserId("1");
user1.setUserName("userName1");
UserBean user2= new UserBean();
user2.setUserId("2");
user2.setUserName("userName2");
UserBean user3= new UserBean();
user3.setUserId("3");
user3.setUserName("userName3");
userList = new ArrayList<UserBean>();
userList.add(user1);
userList.add(user2);
userList.add(user3);
}
public void moveUserDown(){
if(user !=null){
int indexObj= userList.indexOf(user);
if(indexObj < userList.size()-1){
UserBean tempUser=userList.get(indexObj+1);
userList.set(indexObj+1, user);
userList.set(indexObj, tempUser);
}
}
}
public void moveUserUp(){
if(user !=null){
int indexObj= userList.indexOf(user);
if(indexObj > 0){
UserBean tempUser=userList.get(indexObj-1);
userList.set(indexObj-1, user);
userList.set(indexObj, tempUser);
}
}
}
}
UserBean
import java.io.Serializable;
public class UserBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3820279264217591645L;
private String userName;
private String userId;
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the userId
*/
public String getUserId() {
return userId;
}
/**
* @param userId the userId to set
*/
public void setUserId(String userId) {
this.userId = userId;
}
}