单击链接时调用函数

时间:2011-02-18 04:06:28

标签: java javascript jquery jsf java-ee

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

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

然后,这是托管bean show()中的方法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,当我点击链接时,它会调用一个方法,它将返回一个String,将我重定向到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页面[CentralFeed.jsf,Profile.jsf,TextBook.jsf]使用相同的标题,所以我为标题创建了一个模板页面,如下所示。

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此菜单按钮绑定到Session Scoped Managed Bean。当您单击此菜单按钮的项目时,您将重定向到三个jsf页面之一。但我也想让菜单按钮正确反映我所看到的页面。所以这是我的尝试

SessionBean.java ---&gt; Session Scoped Managed 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页中的任何一页导航,我也可以导航到其他页面,我仍然希望菜单按钮正确反映我正在查看的页面。那么让我们来看看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,但不允许我更改菜单按钮的值。请帮忙

3 个答案:

答案 0 :(得分:1)

如果您使用的是JSF 2.0,那么您应该将这些HTML生成片段作为复合组件进行研究。 h:outputText用于生成人类可视文本,根据我的经验,禁用转义的属性很脆弱。

答案 1 :(得分:1)

您想要的是阻止浏览器加载HREF属性并执行JS代码。你可以这样做:

<a href="#" onclick="runScript()">

然后只需定义runScript()函数来执行您想要的任何操作(业务逻辑),最后加载新页面:

function runScript(Long id)
{
    // business logic
    // ...

    location.href = 'Profile.jsf?faces-redirect=true&amp;userId=' + id;
}

答案 2 :(得分:1)

只需在构造函数中执行所需的作业,或者在EL中与@PostConstruct关联的托管bean中执行Profile.jsf。您可以使用@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包含模板或复合组件。普通的vanilla HTML并不属于Model,而是属于View。

相关问题