1

我有一个具有两种方法的控制器类。假设“http://localhost:8080/SpringAOP1/welcome/two/aa”,一旦 URL 被击中,第一种类型必须执行其主体。这里 SpringAOP1 是我的项目,welcome 是控制器类名(带有注释设置),二是带有参数“aa”的函数名。第二种类型的函数需要由拦截器预先处理。因此,当 URL 被命中假设“http://localhost:8080/SpringAOP1/welcome/intercep/six/aa”时,必须调用拦截器。在这里,我将“intercep”参数与所有需要拦截器调用的第二类函数一起传递。

控制器类:-


@Controlle
 @RequestMapping("/welcome")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome1(ModelMap model) {

    model.addAttribute("message", "Spring 3 MVC Hello World 1");
    return "hello";

}
@RequestMapping( value="two/{name}", method = RequestMethod.GET)

public String printWelcome2(@PathVariable String name,ModelMap model) {

    model.addAttribute("message", "Spring 3 MVC Hello World 2");
    model.addAttribute("value", name);
    return "hello";

}

@RequestMapping( value="intercep/six/{name}", method = RequestMethod.GET)

public String printWelcome6(@PathVariable String name,ModelMap model) {

    model.addAttribute("message", "Spring 3 MVC Hello World 6");
    model.addAttribute("value", name);
    return "hello";

}

`

调度程序 servlet

 <beans xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.mkyong.common.controller" />

<bean name="HijackBeforeMethod" class="com.mkyong.common.controller.HijackBeforeMethod"/>
 <mvc:interceptors>
<mvc:interceptor>
    <mvc:mapping path="/intercep/*"/>
    <bean id="HijackBeforeMethod" class="com.mkyong.common.controller.HijackBeforeMethod" />
</mvc:interceptor>
 </mvc:interceptors>

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="order" value="0" />
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>


HijackBefore 方法

(这是被调用的拦截器类)

 public class HijackBeforeMethod extends HandlerInterceptorAdapter {
public Boolean comp(String u,String p)
{
    Map mMap = new HashMap();
    mMap.put("aa", "bb");
    mMap.put("mm", "nn");
    mMap.put("xx", "yy");


    Iterator iter = mMap.entrySet().iterator();
    String k,v;
    Boolean flg=false;
    while (iter.hasNext())
    {
        Map.Entry mEntry = (Map.Entry) iter.next();
        if(mEntry.getKey().equals(u) && mEntry.getValue().equals(p))
          flg=true;
    }
    return flg;
}


public boolean preHandle(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse, Object o) throws Exception {
    String u="aa";
    String p="bb";



    //System.out.println(" The username is--> "+u+"  and pass is --> "+p);
    Boolean ch=comp(u,p);
            if(ch)
              {
                if(httpServletRequest.getMethod().equalsIgnoreCase("get")){
                    String uri = httpServletRequest.getRequestURI();
                    System.out.println("The method is GET :: "+ uri);
                }

            else
              {
                return false;   
               }

    }


}

请帮我找到这个。我有很大的麻烦..

4

1 回答 1

0

您可以使用 WebRequestInterceptor 接口代替 HandlerInterceptorAdapter。以下代码段对我来说工作正常。

public class FlashInterceptor implements WebRequestInterceptor {
    private Flash flash;

    @Autowired
    public void setFlash(Flash flash) {
            this.flash = flash;
    }

    @Override
    public void preHandle(WebRequest request) throws Exception {
            final Map<String, Object> messages = flash.getMessages();
            if (messages.containsKey("flash_info")) {
                    request.setAttribute("flash_info", messages.get("flash_info"),
                                    RequestAttributes.SCOPE_REQUEST);
                    request.setAttribute("flash_info_params",
                                    messages.get("flash_info_params"),
                                    RequestAttributes.SCOPE_REQUEST);
            }
            if (messages.containsKey("flash_error")) {
                    request.setAttribute("flash_error", messages.get("flash_error"),
                                    RequestAttributes.SCOPE_REQUEST);
                    request.setAttribute("flash_error_params",
                                    messages.get("flash_error_params"),
                                    RequestAttributes.SCOPE_REQUEST);
            }
            if (messages.containsKey("flash_success")) {
                    request.setAttribute("flash_success",
                                    messages.get("flash_success"),
                                    RequestAttributes.SCOPE_REQUEST);
                    request.setAttribute("flash_success_params",
                                    messages.get("flash_success_params"),
                                    RequestAttributes.SCOPE_REQUEST);
            }
            flash.reset();
    }

    @Override
    public void postHandle(WebRequest request, ModelMap model) throws Exception {

    }

    @Override
    public void afterCompletion(WebRequest request, Exception ex)
                    throws Exception {
    }

}
于 2012-12-06T07:30:46.727 回答