CommandButton问题

时间:2013-07-11 22:17:04

标签: java jsf jsf-2

我有这个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”,因此没有重定向,但仍然没有执行该方法。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

更改type="button"的{​​{1}}属性或删除它,因为type="submit"是标记的默认行为。 type="submit"通常用于execute client side methods或Ajax调用。这里有another post by BalusC