【问题标题】:Spring Controller doesn't load the correct viewSpring Controller 没有加载正确的视图
【发布时间】:2018-02-22 17:03:15
【问题描述】:

我正在研究春天。我的控制器AdminProduitsController(下面的代码)没有加载正确的视图。它应该加载一个名为produits.jsp 的页面。相反,它会加载另一个名为AdminCategorieController 的控制器的页面categories.jsp。两个控制器属于同一个包。 页面produits.jspcategories.jsp 在同一个目录中。

AdminProduitsController

@Controller
@RequestMapping(value="/adminProd")
public class AdminProduitsController {

    @Autowired
    private IAdminProduitsMetier metier;

    @RequestMapping(value="/index")
    public String home(Model model)
    {
        model.addAttribute("produits",metier.listProduits());
        model.addAttribute("categories",metier.listCategories());
        model.addAttribute("produit", new Produit());
        return "produits";
    }
}

AdminCategorieController

@Controller
@RequestMapping(value = "/adminCat")
public class AdminCategorieController implements HandlerExceptionResolver{

    @Autowired
    IAdminCategoriesMetier metier;

    @RequestMapping(value="/index")
    public String home(Model model)
    {
        model.addAttribute("categorie",new Categorie());
        model.addAttribute("categories",metier.listCategories());
        return "categories";
    }
}

这里是配置文件servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <annotation-driven />

    <resources mapping="/resources/**" location="/resources/" />

    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="org.aaronlbk.eboutique" />

    <beans:bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <beans:property name="maxUploadSize" value="100000"></beans:property>
    </beans:bean>

</beans:beans>

jsp 文件完全不同。

categories.jsp

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="f"%>

<head>
<!-- .getContextPath() contexte du projet -->
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/css/style1.css">
<link href="<c:url value="/resources/css/style1.css" />" rel="stylesheet">
</head>

<body>
<div id="formCat" class="cadre">

    <f:form modelAttribute="categorie" action="saveCat" method="post"
        enctype="multipart/form-data">

        <table class="tab1">
            <tr>
                <td>ID categorie</td>
                <td><f:input path="idCategorie" />
                <td><f:errors path="idCategorie" cssclass="errors"></f:errors></td>
            </tr>
            <tr>
                <td>Nom categorie</td>
                <td><f:input path="nomCategorie" />
                <td><f:errors path="nomCategorie" cssclass="errors"></f:errors></td>
            </tr>
        </table>
    </f:form>
</div>
</body>

produits.jsp

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="f"%>

<head>
<link href="<c:url value="/resources/css/style1.css" />" rel="stylesheet">
</head>

<body>

<div id="formProd" class="cadre">

    <f:form modelAttribute="produit" action="saveProd" method="post"
        enctype="multipart/form-data">

        <table class="tab1">
            <tr>
                <td>ID Produit</td>
                <td><f:input path="idProduit" />
                <td><f:errors path="idProduit" cssclass="errors"></f:errors></td>
            </tr>
            <tr>
                <td>Catégorie</td>
                <td><f:select path="categorie.idCategorie" items="${categories}" itemValue="idCategorie" itemLabel="nomCategorie"/>
                <td><f:errors path="designation" cssclass="errors"></f:errors></td>
            </tr>
        </table>
    </f:form>
</div>

</body>

为什么会导致控制器AdminProduitsController返回另一个控制器的页面?

【问题讨论】:

    标签: java jsp spring-mvc controller


    【解决方案1】:

    您能否分享您为调用控制器而点击的 URL?

    也许你可以试试 ModelAndView("viewname")

    @RequestMapping(value="/index")
    public ModelAndView home(Model model)
    {
        model.addAttribute("produits",metier.listProduits());
        model.addAttribute("categories",metier.listCategories());
        model.addAttribute("produit", new Produit());
        return new ModelAndView("produits");
    }
    

    【讨论】:

    • 这看起来是正确的。您可以尝试的几件事 1. 调试并放置 breakput 并检查它是否调用了哪个控制器。 2.如果你使用tomcat,请删除work/Catalina/localhost目录后重试(停止tomcat,删除再启动) 3.试试ModelAndView。
    • 我已经完成了 3 点。我仍然有这个问题。很奇怪
    • 该问题与执行期间引发的异常有关,但我没有很好地管理。非常感谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多