【问题标题】:Spring MVC controller 404 [duplicate]Spring MVC控制器404 [重复]
【发布时间】:2016-06-29 12:55:50
【问题描述】:

我使用 Spring MVC。

当我点击 jsp 链接时,会加载其他 jsp。

我有一些控制器。

我也用

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

当我点击 jsp 链接时,我得到一个 404

项目结构是

我的jsp是

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<title>Spring MVC Tutorial Series by Crunchify.com</title>
<style type="text/css">
body {
    background-image: url('http://crunchify.com/bg.png');
}
</style>
</head>
<body>
    <br>
    <div style="text-align:center">
        <h2>
            Hey You..!! This is your 1st Spring MCV Tutorial..<br> <br>
        </h2>
        <h3>


        <a href="<c:url value="/EusurveyAdminB/welcome"/>">enlace</td>

        </h3>
    </div>
</body>
</html>

WelcomeController.java 是

package eusurvey.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;



import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import eusurvey.modelA.daos.Preferencia;
import eusurvey.services.PreferencesService;

@Controller
@RequestMapping("/welcome")
public class WelcomeController {
    private static final Logger logger = Logger.getLogger(WelcomeController.class);

    @Resource(name = "preferencesService")
    private PreferencesService preferencesService;

    private int a = 0;
    private  static List<Preferencia> results = null;



    /*@RequestMapping
    public ModelAndView consultaPreferencia(ModelMap model){
        logger.info("PreferencesControles.- Principio de consultaPreferencia");

        results = preferencesService.consultaPreferencia();
        logger.info("PreferencesControles.- consultaPreferencia results size "+results.size());
        return new ModelAndView("welcome", "preferencias",results);

    }*/

    @RequestMapping(method = RequestMethod.GET)
        public ModelAndView welcome(HttpServletRequest request, Model model)  { 
        logger.info("PreferencesControles.- Principio de consultaPreferencia");

        results = preferencesService.consultaPreferencia();
        logger.info("PreferencesControles.- consultaPreferencia results size "+results.size());
        return new ModelAndView("welcome", "preferencias",results);

    }


}

调度员是

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.jasypt.org/schema/encryption
        http://www.jasypt.org/schema/encryption/jasypt-spring31-encryption-1.xsd">

    <context:annotation-config /> 
    <import resource="hibernate-context.xml" />
    <bean id="propertyPlaceholderConfigurer"
        class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
        <constructor-arg ref="configurationEncryptor" />
        <property name="location" value="/WEB-INF/spring.properties" />
    </bean>

    <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
        <property name="config" ref="environmentVariablesConfiguration" />
    </bean>

    <bean id="environmentVariablesConfiguration"
        class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
        <property name="algorithm" value="PBEWITHSHA256AND256BITAES-CBC-BC" />
        <property name="passwordEnvName" value="CAS_PBE_PASSWORD" />
        <property name="providerClassName"
            value="org.bouncycastle.jce.provider.BouncyCastleProvider" />
        <property name="providerName" value="BC" />
    </bean>



   <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />


    <bean id="messageSource"
            class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="WEB-INF/classes/messages" />
    </bean>

    <context:component-scan base-package="eusurvey" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

控制器在 eusuvey.controller 包中

在调度程序中我扫描包裹 eusuvey

错误是

我必须如何定义 jsp、控制器和调度程序来修复这个 404?

【问题讨论】:

  • 注解驱动的 mvc 和 ControllerClassNameHandlerMapping 不兼容。请改用&lt;mvc:annotation-driven /&gt;

标签: spring jsp hyperlink controller


【解决方案1】:

当使用@RequestMapping 注释时,您宁愿需要org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping(spring 2)或org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping(spring 3)之类的东西。

只需使用 &lt;mvc:annotation-driven /&gt; 配置元素启用它(参见 M.Deinum 注释)。

【讨论】:

    猜你喜欢
    • 2018-07-22
    • 2021-04-01
    • 2011-01-05
    • 2023-03-27
    • 2014-07-15
    • 2020-03-27
    • 1970-01-01
    • 2011-08-16
    • 2016-09-17
    相关资源
    最近更新 更多