简体   繁体   English

从jsf页面将数据传输到servlet

[英]transfer data to a servlet from a jsf page

I have an input in my jsf page like this 我在jsf页面中有这样的输入

<html:inputText id="ResponseOK" value="#{bean.ResponseOK}" binding="#{bean.ResponseOKInput}" /> 

I want to get the value in a servlet (by request.getParameter ("ResponseOK")) when i click on a command button 当我单击命令按钮时,我想在servlet中获取值(通过request.getParameter(“ ResponseOK”))

<html:commandButton value="Valider" action="#{bean.callServlet}"/>

which call a function 哪个调用函数

public void callServlet()
    {
         String url = "http://localhost:8080/TestOne/Timers";  //servlet
            FacesContext context = FacesContext.getCurrentInstance();  
            try {  

               context.getExternalContext().redirect(url);  

            }catch (Exception e) {  
               e.printStackTrace();  
            }  
            finally{  
               context.responseComplete();  
            }  
    }

Unfortunately, in my servlet the variable Ok , return only null 不幸的是,在我的servlet中,变量Ok只能返回null

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        String Ok = request.getParameter("ResponseOK");// return null
        System.out.println(timerOk);
        }

thank you very much 非常感谢你

In order for you to be able to get a property from the request, the input must have the name attribute: 为了使您能够从请求中获取属性,输入必须具有name属性:

<html:inputText id="ResponseOK" value="#{bean.ResponseOK}" binding="#{bean.ResponseOKInput}"  name="ResponseOK"/>

UPDATE: 更新:

I'm not too familiar with the JSF framework but i think your problem is the action button. 我不太熟悉JSF框架,但我认为您的问题是操作按钮。

This button is not a submit button so the value of the input is not being sent to the request. 此按钮不是“提交”按钮,因此输入的值未发送到请求。

When calling a GET request, you need to pass the parameter in URL itself, so you need the url to look like: 调用GET请求时,您需要在URL本身中传递参数,因此您需要的URL如下所示:

http://localhost:8080/TestOne/Timers?ResponseOK=value

So you need to transfer the value of the ResponseOK input to the callServlet method. 因此,您需要将ResponseOK输入的值传输到callServlet方法。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM