我不能用我的JSP映射我的Servlet

时间:2012-01-17 22:22:45

标签: java jsf servlets mapping commandlink

使用JSF框架,特别是commandLink标记,我的servlet与Java Server Page的映射存在问题。

当我点击commandLink时,它只重新加载相同的productList.jsp。

这是我的地图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>

这是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>

这是我创建新产品的链接(访问editProduct.jsp页面)

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

前缀h代表

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

最后我的Servlet中的doGet和doEditProduct方法:

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

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

使用JSF,您不能使用servlet。您使用托管bean。

为了修复上面的代码,您应该提供一个操作方法,并从命令按钮调用该方法。为了更多地了解JSF的精神,我建议从教程和/或示例JSF项目开始。