Srikanth
Srikanth

Reputation: 163

Action method is invoked without pressing submit button

I have this form:

<h:form id="testForm"> 
    <p:fieldset legend="data">  
        <p:dataTable id="fileData" var="dataList" value="#{fileUpload.displayData}">  
            <p:column headerText="Htno">  
                <h:outputText value="#{dataList.htno}" />  
            </p:column>  

            <p:column headerText="Ecode">  
                <h:outputText value="#{dataList.ecode}" />  
            </p:column>  

            <p:column headerText="Subcode">  
                <h:outputText value="#{dataList.subcode}" />  
            </p:column>  

            <p:column headerText="Imf">  
                <h:outputText value="#{dataList.imf}" />  
            </p:column> 

            <p:column headerText="Action"> 
            <!-- <h:commandLink value="Upload" action="#{fileUpload.single}" rendered="true" >
                <f:param name="imf" value="#{dataList.imf}" />
                <h:inputHidden value="#{fileUpload.imf}" id="imf" />
            </h:commandLink> -->
            <h:commandButton image="delete" ajax="false" style="margin-right:20px;" action="#{fileUpload.single}" ></h:commandButton>
            </p:column>
        </p:dataTable>
        <h:inputHidden value="#{fileUpload.uploadFileName}" id="uploadFileName" />
        <h:inputHidden value="#{fileUpload.dirPath}" id="dirPath" />
        <h:commandButton type="submit" value="Upload All" action="#{fileUpload.uploadAll}"></h:commandButton>  
    </p:fieldset> 
</h:form>

and this bean:

public class FileUpload {

    public String single() {
        System.out.println("-----------**---d---");
        return "home";
    }
}

It's causing this exception:

javax.servlet.ServletException: The class 'com.primefaces.sample.FileUpload' does not have the property 'single'

If I make it a property with a getter and setter, then the exception isn't thrown anymore. However it's invoked during page load without pressing the submit button. I can't for life figure out the cause.

Upvotes: 2

Views: 1086

Answers (1)

BalusC
BalusC

Reputation: 1109532

Your problem is caused here:

<!-- <h:commandLink value="Upload" action="#{fileUpload.single}" rendered="true" >
    <f:param name="imf" value="#{dataList.imf}" />
    <h:inputHidden value="#{fileUpload.imf}" id="imf" />
</h:commandLink> -->

HTML comments are interpreted as template text. They also need to end up in the generated HTML output. HTML comments doesn't stop EL expressions from being evaluated. It's exactly like as if you're doing:

<p>Blah blah #{fileUpload.single} blah blah</p>

This is interpreted as <h:outputText value="#{fileUpload.single}"/> which obviously requires a getter.

You need to remove the whole comment, or to wrap it in <ui:remove>, or tell Facelets to skip comments during parsing by the following context parameter in web.xml:

<context-param>
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
</context-param>

Upvotes: 1

Related Questions