Spring MVC控制器404 [英] Spring MVC controller 404

查看:87
本文介绍了Spring MVC控制器404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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吗?

解决方案

使用@RequestMapping批注时,您宁愿使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping(弹簧2)或org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping(弹簧3)之类的东西. >

只需使用<mvc:annotation-driven />配置元素(参见M.Deinum注释)启用它即可.

I work with Spring MVC.

When I click in the jsp link other jsp is loaded.

I have some controllers.

Also I use

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

When I click the jsp link I get a 404

Project structure is

My jsp is

<%@ 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 is

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

    }


}

Dispatcher is

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

Controller are in the packages eusuvey.controller

In the dispatcher I scan the package eusuvey

The error is

How do I have to define jsp, controller and dispatcher to fix this 404?

解决方案

When using @RequestMapping annotations, you'd rather need something like the org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping (spring 2) or org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping (spring 3).

Just enable it with an <mvc:annotation-driven /> config element (Cf. M.Deinum comment).

这篇关于Spring MVC控制器404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆