0

I have this JSF code

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

I also have this bean

@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();
        }   

    }
}

When I click the button, the submit method is not executed. Seems as if the button doesn't do anything. Since I set it as type="button", there is no redirect, but still, the method isn't executed.

Any ideas?

Nacho321
  • 1,911
  • 7
  • 33
  • 55
  • How did you noticed that the method is never invoked? Did you added a log message or something? – Luiggi Mendoza Jul 11 '13 at 22:22
  • Yes, on the server's side, there's a log that registers every execution. – Nacho321 Jul 11 '13 at 22:24
  • I meant if you added a log message **inside** your action method. By the way, review the possible reasons on http://stackoverflow.com/q/2118656/1065197 – Luiggi Mendoza Jul 11 '13 at 22:25
  • No, there's no log. How can I add one to double check if the method's been called at all? (Sorry, I'm kinda new to JSF) – Nacho321 Jul 11 '13 at 22:27
  • You should have a logger configured in your application like Log4j2 or LogBack (note that a logger is completely unrelated with JSF). If you have none in your application, then use plain `System.out.println("this should be in a logger")` until you configure one. – Luiggi Mendoza Jul 11 '13 at 22:29
  • When you use `action` it is expecting a String not a void return method. You can make a `return null;` if you need. – danRod Jul 11 '13 at 22:50
  • @danRon that's from ye olde JSF 1.x. Since JSF 1.2 you can pass `void` methods to `action`. – Luiggi Mendoza Jul 12 '13 at 02:11
  • I added log4j. I've checked with other methods, and the logging works fine. But the method in question is never executed. – Nacho321 Jul 12 '13 at 14:34

1 Answers1

2

Change the type="button" attribute for type="submit" or just remove it, as type="submit" is the default behaviour of the tag. type="button" is normally used to execute client side methods or Ajax calls. Here you have another post by BalusC.

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217