我无法将 Servlet 映射到 JSP

发布于 2024-12-27 13:23:48 字数 6865 浏览 1 评论 0原文

我在使用 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);
}

谢谢您帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬心 2025-01-03 13:23:48

对于 JSF,您不得使用 servlet。您使用托管 Bean。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文