0

我有这个 JSF 代码

<f:view>
    <h:form>
        <h:commandButton value="Submit info" type="button" action="#{bean.submit}" />
    </h:form>
</f:view>

我也有这个豆子

@ManagedBean(name="bean")
@RequestScoped
public class Bean{
    public void submit(){
        HttpURLConnection connection = null;
        URL url;
        String generatedUrl = "blalabla"; //Long url
        StringBuffer response = new StringBuffer();
        try {
            url = new URL(generatedUrl);


            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");

            int responseCode = connection.getResponseCode();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;


            while((inputLine = in.readLine()) != null){
                response.append(inputLine);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }   

    }
}

当我单击按钮时,不执行提交方法。似乎按钮没有做任何事情。由于我将其设置为 type="button",因此没有重定向,但仍然没有执行该方法。

有任何想法吗?

4

1 回答 1

2

更改或删除它的type="button"属性,type="submit"就像type="submit"标签的默认行为一样。type="button"通常用于执行客户端方法或 Ajax 调用。在这里,您还有BalusC 的另一篇文章

于 2013-07-12T21:12:24.263 回答