java - 单击链接时调用函数

标签 java javascript jquery jsf jakarta-ee

因此,在我的 JSF 页面中,我有一个 outputText,其内容是由托管 bean 生成的。

<h:outputText value="#{bean.show(id)}" />

那么这里是方法show()托管 bean 内部 bean .

public String show(Long fromUserId){
     User from = myEJB.findUserById(fromUserId);  
     String content = "";
     //This method contains complex business logics, below are just an illustration
     if(...){
         content += ... ;  //generate some content here
         content += "<a href=\"Profile.jsf?userId=" + fromUserId + "\">";
         content += from.getFname() + " " + from.getLname() + "</a>
     }else if(...){
         //code that required to access database
         content += ...;
     }else if(...){
         content += ...;
     }
     ...
     return content;
}

因此,在我的页面上,我有一些内容和一个链接到我的个人资料页面的链接。我想要的是,当我单击链接时,我想调用一个方法,该方法将在将我重定向到 Profile.jsf 页面之前运行一些业务逻辑。所以我想我需要编写一些 javascript,当我单击链接时,它将调用一个方法,该方法将返回一个字符串,将我重定向到 Profile.jsf。我尝试了这个,但不起作用

public String show(Long fromUserId){
     ...
     content += "<a href=\"#\" onclick=\"goToProfile(1)\">";
     content += from.getFname() + " " + from.getLname() + "</a>
     return content;
}

public String gotoProfile(Long id){
     //Some business logic
     return "Profile.jsf?faces-redirect=true&amp;userId=" + id;
}

请帮忙

编辑

@BalusC:我尝试了你的建议,但由于我的设计方式,它不起作用。原因如下:我有 3 View Scoped Managed Bean使用相同 header 的页面 [CentralFeed.jsf、Profile.jsf、TextBook.jsf],因此我为 header 创建一个模板页面,如下所示。

template-header.xhtml

<h:form style="display: inline;">
    <p:menuButton value="#{SessionBean.option}" >                                        
        <p:menuitem value="My Profile" ajax="false" action="#{SessionBean.goToProfile}" />
        <p:menuitem value="Stanford University" ajax="false" action="#{SessionBean.goToCentral}" />
        <p:menuitem value="Universal Trade" ajax="false" action="#{SessionBean.goToTrade}" />
   </p:menuButton>
</h:form> 
<ui:insert name="body"/>

那么我的三个jsf页面将使用ui:compositionui:define包含模板标题。所以我的模板页面的标题有一个 Primefaces menu button此菜单按钮绑定(bind)到 Session Scoped Managed Bean 。当您单击此菜单按钮的某一项时,您将被重定向到三个 jsf 页面之一。但我也希望菜单按钮能够正确反射(reflect)我正在查看的页面。这是我的尝试

SessionBean.java ---> session 范围托管 Bean

public static final String MY_PROFILE = "My Profile";
public static final String CENTRAL_FEED = "Stanford University";
public static final String TRADE = "Universal Trade";
private String option = null;

@PostConstruct
public void init(){        
    me = scholarEJB.findUserByUserName(getLoginUserName());                
    if(me != null){        
    HttpServletRequest request =
            (HttpServletRequest) FacesContext.getCurrentInstance().
                getExternalContext().getRequest();
    String path = request.getServletPath();
    if(path.contains("Profile")){
        option = MY_PROFILE;
    }else if(path.contains("CentralFeed")){
        option = CENTRAL_FEED;
    }else if(path.contains("TextBook")){
        option = TRADE;
    }else{
        option = " ";
    }
} 
public String goToProfile(){
    option = MY_PROFILE;
    return "Profile.jsf?faces-redirect=true&amp;userId="+me.getId();
}
public String goToCentral(){
    option = CENTRAL_FEED;
    return "CentralFeed.jsf?faces-redirect=true";
}

public String goToTrade(){
    option = TRADE;
    return "TextBook.jsf?faces-redirect=true";
}

如果我使用菜单按钮导航,那么这是可行的,但从 3 个页面中的任何一个,我也可以导航到其他页面,并且我仍然希望菜单按钮能够正确反射(reflect)我正在查看的页面。那么让我们看一下 CentralFeed.jsf。因此,对于 Profile.jsf 的链接,我会这样做

<p:commandLink value="Profile"
        action="#{CentralFeed.goToProfile(CentralFeed.me.id)}" ajax="false"/>

所以在 CentralFeed.java 中,我有:

public String goToProfile(Long userId){
    sessionBean.setOption(SessionBean.MY_PROFILE);  //sessionBean: Session Scoped Bean reference
    return "Profile.jsf?faces-redirect=true&amp;userId=" + userId;
}

当我单击链接时,我重定向到 Profile.jsf,菜单按钮的值为 MY_PROFILE 。这项工作,只有当我必须执行大量业务逻辑来弄清楚输出应该是什么时,我才会遇到问题。这就是我在上面的原始代码中尝试做的事情。根据某些条件,我将生成 HTML。但现在我通过 <a href="..."> 生成的链接,只会直接带我到Profile.jsf ,但不允许我更改菜单按钮的值。请帮忙

最佳答案

只需在构造函数中或在通过 EL 与 Profile.jsf 关联的托管 bean 中的 @PostConstruct 中执行所需的工作即可。您可以使用 @ManagedProperty 让 JSF 在托管 bean 中设置请求参数。

@ManagedProperty(value="#{param.userId}")
private Long userId;

@PostConstruct
public void init() {
    // Do business stuff based on userId.
}
<小时/>

与具体问题无关,我必须说我同意 Thorbjørn 的观点。我知道您已经在使用 JSF 2.0/Facelets。只需为此创建一个包含模板或复合组件的 Facelets。普通 HTML 并不真正属于模型,而是属于 View 。

关于java - 单击链接时调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5037491/

相关文章:

java - 即使 onDestroy 被触发,我的服务也不会停止

java - 解析来自 ejb 代理的注释元数据

javascript - 在拖动操作未完成之前,浏览器不遵守新的 css 设置

java - 套接字代码创建新的作家?

php - 重组我的对象的最佳方法

javascript - 为什么这个 NULL 变量是真实的?

javascript - 删除动态前置的 div

javascript - 动画倒计时/到特定数字

javascript - 在 javascript 中单击时更改 div 高度?

java - Spring 批处理 - 重新启动使用 TaskExecutor 的失败作业