简体   繁体   English

Spring MVC拦截器没有被调用

[英]Spring MVC INterceptor is not being called

I am writing test to check the working of spring MVC interceptor. 我正在编写测试以检查Spring MVC拦截器的工作。 I have my working controller for which i have introduced interceptor code. 我已经为我的工作控制器引入了拦截器代码。 Everything is working fine except interceptor is not being executed. 一切工作正常,除了没有执行拦截器。 No error nothing. 没错没事。

Interceptor: 拦截器:

package com.neha.javabrains;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Component
public class TimeBasedInterceptor extends HandlerInterceptorAdapter{

    private int openingTime=9;
    private int closingTime=18;
    public int getOpeningTime() {
        return openingTime;
    }
    public void setOpeningTime(int openingTime) {
        this.openingTime = openingTime;
    }
    public int getClosingTime() {
        return closingTime;
    }
    public void setClosingTime(int closingTime) {
        this.closingTime = closingTime;
    }

    public boolean preHandle(HttpServletRequest httpRequest,HttpServletResponse httpResponse) throws Exception
    {
        System.out.println("here in interceptor");
        Calendar cal=Calendar.getInstance();
        int hour=cal.HOUR_OF_DAY;
        if(openingTime<=hour && hour<closingTime)
        {
            return true;
        }
        httpResponse.sendRedirect("http://localhost:8080/SpringWebProject/outsideHours.html");
        return false;   
    }
}

Application Context entry: 应用程序上下文条目:

<context:component-scan base-package="com.neha.javabrains" />
<context:annotation-config/>  
<mvc:annotation-driven /> 
<mvc:default-servlet-handler />

<mvc:resources mapping="/resources/**" location="/resources/" />
<tx:annotation-driven transaction-manager="txManager"/>
<aop:aspectj-autoproxy />

  <mvc:interceptors>

     <bean class="com.neha.javabrains.TimeBasedInterceptor">
     </bean>

  </mvc:interceptors>

Controller: 控制器:

package com.neha.javabrains;

import java.util.Locale;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {

public LoginService loginService;

@Autowired
@Qualifier("loginFormValidator")    
public Validator validator;
@Autowired
public MessageSource messageSource;

@InitBinder
private void initBinder(WebDataBinder binder) {
    binder.setValidator(validator);
}

@Autowired
private Environment environment;

@RequestMapping("/")    
public String doLogin(ModelMap modelMap)
{
    System.out.println("doLogin" +loginService);

    String message=messageSource.getMessage("message", null, "default", Locale.UK);
    System.out.println("Message is:" + message);
    LoginForm loginForm=new LoginForm();
    modelMap.put("loginForm",loginForm);
    return "login";
}

public void printData()
{
    System.out.println("just to test aspect programming");
}

@RequestMapping("/login")   
public String verifyLogin(@Valid @ModelAttribute("loginForm") LoginForm loginForm,BindingResult bindingResult)
{
    if(bindingResult.hasErrors())
    {
        System.out.println("here in error" + bindingResult.getAllErrors().get(0));
        return "login";
    }
    loginService.verifyLogin(loginForm);
    LoginForm loginFrm=new LoginForm();
    loginFrm.setUsername(loginForm.getUsername());
    loginFrm.setPassword(loginForm.getPassword());
    ModelAndView mav=new ModelAndView();
    mav.addObject("loginForm", loginFrm);
    return "result";
}

@RequestMapping("/addUser") 
public String addUser(@ModelAttribute("LoginForm") LoginForm loginForm,LoginForm loginFrm)
{
    System.out.println("In add User");
    loginService.addUser(loginForm);
    loginFrm.setUsername(loginForm.getUsername());
    loginFrm.setPassword(loginForm.getPassword());
    ModelAndView mav=new ModelAndView();
    mav.addObject("LoginForm", loginFrm);
    return "result";
}

public LoginService getLoginService() {
    return loginService;
}
@Autowired
@Required
public void setLoginService(LoginService loginService) {
    this.loginService = loginService;
    System.out.println("i was called" + loginService);

}

public Environment getEnvironment() {
    return environment;
}

public void setEnvironment(Environment environment) {
    this.environment = environment;
}

public MessageSource getMessageSource() {
    return messageSource;
}

public void setMessageSource(MessageSource messageSource) {
    this.messageSource = messageSource;
}



}

Web.xml entry is: Web.xml条目是:

<web-app id="WebApp_ID" version="2.4"
    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">
    <servlet>

    <servlet-name>Neha</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext*.xml</param-value>
        </init-param>
       <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>Neha</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>dev</param-value>
    </context-param>
    <listener>  
   <listener-class>
       org.springframework.web.context.ContextLoaderListener
   </listener-class>
  </listener>
</web-app>

Can anyone please help..... 谁能帮忙.....

You seem to have missed the path where you want to apply the interceptor. 您似乎已经错过了要应用拦截器的路径。

Try this 尝试这个

<mvc:interceptors>
   <mvc:interceptor>
       <mvc:mapping path="/**" /> <!-- Specify here what all path you want to intercept -->
       <bean class="com.neha.javabrains.TimeBasedInterceptor" />
   </mvc:interceptor>
</mvc:interceptors>

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM