1

I have a problem for mapping my servlet with my Java Server Page, using the JSF framework and especially the commandLink tag.

When I click on the commandLink it only reload the same productList.jsp.

Here is my map web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>Epimarket</display-name>
<servlet>
    <servlet-name>mainServlet</servlet-name>
    <servlet-class>com.epimarket.controller.EpimarketServlet</servlet-class>
    <init-param>
        <param-name>listURL</param-name>
        <param-value>productList.jsp</param-value>
    </init-param>
    <init-param>
        <param-name>editURL</param-name>
        <param-value>productEdit.jsp</param-value>
    </init-param>
    <init-param>
        <param-name>errorsURL</param-name>
        <param-value>errors.jsp</param-value>
    </init-param>
</servlet>

<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
    <init-param> 
        <param-name>maxFileSize</param-name>
        <param-value>20m</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

<filter-mapping>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <url-pattern>/faces/myFacesExtensionResource/*</url-pattern>
</filter-mapping>

<context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
    <param-value>false</param-value>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
    <param-value>true</param-value>
</context-param>

<servlet-mapping>
    <servlet-name>mainServlet</servlet-name>
    <url-pattern>/do/*</url-pattern>
</servlet-mapping>


<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

Here is the faces-config.xml

<?xml version="1.0"?>
<!DOCTYPE   faces-config PUBLIC
        "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
        "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<application>
    <locale-config>
        <default-locale>fr</default-locale>
    </locale-config>
</application>

<navigation-rule>
    <from-view-id>/*</from-view-id>
    <navigation-case>
        <from-outcome>productList</from-outcome>
        <to-view-id>/productList.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-outcome>productEdit</from-outcome>
        <to-view-id>/productEdit.jsp</to-view-id>
    </navigation-case>
    <navigation-case>
        <from-outcome>chart</from-outcome>
        <to-view-id>/chart.jsp</to-view-id>
    </navigation-case>
</navigation-rule>

Here is my link to create a new Product (access editProduct.jsp page)

<h:commandLink id="createProductLink" value="Ajouter un produit" action="/do/edit"/>

Prefix h is for

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

And finally my doGet and doEditProduct methods in my Servlet :

public void                 doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException, ServletException
    {
        if (initErrors.size() != 0)
        {
            req.setAttribute("erreurs", initErrors);
            getServletContext().getRequestDispatcher(urlErrors).forward(req, res);
            return ;
        }

        String              reqType = req.getMethod().toLowerCase();
        String              action = req.getPathInfo();

        if (action == null)
            action = "/list";
        if (reqType.equals("get") && action.equals("/list"))
        {
            doProductList(req, res);
            return ;
        }
        if (reqType.equals("get") && action.equals("/delete"))
        {
            doDeleteProduct(req, res);
            return ;
        }
        if (reqType.equals("get") && action.equals("/edit"))
        {
            doEditProduct(req, res);
            return ;
        }
        if (reqType.equals("post") && action.equals("/validate"))
        {
            doValidateProduct(req, res);
            return ;
        }
        doProductList(req, res);
    }

private void                doEditProduct(HttpServletRequest req, HttpServletResponse res)
    throws IOException, ServletException
{
    int     id = Integer.parseInt(req.getParameter("id"));

    Product product = null;
    if (id != -1)
        product = service.getProduct(id);
    else
    {
        product = new Product();
        product.setId(new BigDecimal(-1));
    }

    req.setAttribute("editError", "");
    req.setAttribute("id", product.getId());
    req.setAttribute("name", product.getName());
    req.setAttribute("description", product.getDescription());
    req.setAttribute("price", product.getPrice());
    getServletContext().getRequestDispatcher((String)params.get("editURL")).forward(req, res);
}

Thank you for your help

cocoggu
  • 401
  • 7
  • 18

1 Answers1

2

With JSF you must not used servlets. You use managed beans.

In order to fix the above code, you should provide an action method, and invoke that method from your command button. In order to get more into the spirit of JSF, I'd suggest starting with a tutorial and/or a sample JSF probject.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thank you Bozho ! Have I to do all my forms validations in these managed beans ? – cocoggu Jan 17 '12 at 22:55
  • not necessarily - you can use javax.validation for that, or JSF validators. – Bozho Jan 17 '12 at 23:05
  • OK thank you very much. I would like still use my Servlet to do verifications (because I don't have a lot of time). How can I do to map my Servlet with jsf and managed beans ? – cocoggu Jan 17 '12 at 23:29
  • 1
    Forget servlets. Period. It's either JSF+Managedbeans or plain JSP+Servlets. @Bozho: OP is using JSF 1.1 which doesn't support javax.validation. However JSF builtin validation mechanism is more than sufficient and actually better than such a servlet. – BalusC Jan 18 '12 at 03:59