代码之家  ›  专栏  ›  技术社区  ›  user1651129

如何放置request.getParameter(“”)并将其放入bean.method()中?

  •  0
  • user1651129  · 技术社区  · 12 年前

    嗨,我是java和jsp的新手。我无法从jsp中获取我的值。

    这是我的密码。 这些都是在jsp中制作的。

    <h:commandButton action="#{bean1.checkwork}" value="Get Info" type="submit">
       <f:param name="id" value="#{param['image_id']}" /f:param>
    </h:commandButton>
    

    这是我的方法的托管bean代码

    public String checkwork(){
    
        HttpServletRequest request = (HttpServletRequest)FacesContext.
            getCurrentInstance().getExternalContext().getRequest();
        String image_ID = null;
        if(request!=null){
    
            image_ID = request.getParameter("image_id");
    
            images(image_ID);
            student(matric);
    
        } else {
            System.out.println("fail");                    
            return "successful";
        }
    

    很抱歉,也许我在中添加了我的faces-config.xml数据,也许你们会知道发生了什么。因为我添加了你给出的代码,它给了我空值。 脸配置.xml

    <navigation-rule>
        <from-view-id>/MainPage.jsp</from-view-id>
        <navigation-case>
            <from-action>#{bean1.checkwork}</from-action>
            <from-outcome>successful</from-outcome>
            <to-view-id>chicken.jsp?image_id=#{param['image_id']}</to-view-id>
        </navigation-case>
    </navigation-rule>
    
    2 回复  |  直到 12 年前
        1
  •  2
  •   Daniel    12 年前

    <f:param 为此

    <h:commandButton action="#{bean1.work}" value="Get Info" type="submit">
        <f:param name="id" value="#{param['id']}"></f:param>
    </h:commandButton>
    

    。 。 。

    work 方法代码

    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    String id= null;
    if(request!=null){
        id= request.getParameter("id");
    
    }
    
        2
  •  1
  •   f_puras    12 年前

    我想您正在使用JSF。如果是,请在您的问题中添加JSF标签。如果没有,你可以忽略我的回答。

    调用操作时,不需要使用请求数据对其进行参数化。相反,您可以通过 FacesContext 以下为:

    public String checkwork() {
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        Map<String,String> requestParams = ec.getRequestParameterMap();
        final String id = requestParams.get("id");
        ...
    

    称之为使用

    <h:commandButton action="#{bean1.checkwork}" value="Get info" />
    
    推荐文章