Spring mvc控制器处理所有请求,不应该这样做

时间:2014-11-06 10:22:28

标签: spring http spring-mvc servlets

我有两个控制器:

UserController和AdminController。

AdminController正在处理尚未创建的所有请求。还处理访问我的资源的请求。

我也使用spring-security和maven编译所有。

示例:

Request: .../appName/login --> return login view.
Request: .../appName/home --> return home view.
Request: .../appName/fahsjhgasghdjfg --> return admin view.
Request: .../appName/dasjdha/fhfashjfs --> return admin view.
Request: .../appName/resources/css/one.css --> return admin view.

如果我删除AdminController并创建一个名为ExamplefasjkasController的新控制器,它就会发生相同的事情。

代码:

控制器:

@Controller
public class AdminController {

    @RequestMapping(name = "/panel")
    public ModelAndView adminPanel() {
        return new ModelAndView("admin");
    }

}

@Controller
public class UserController extends GenericController {

    @Autowired
    private IUserService userService;

    @RequestMapping(value = { "/", "/home" })
    public ModelAndView home(HttpServletRequest request, Principal principal) {

        ModelAndView model = new ModelAndView(Name.VIEW_HOME);
        model.setViewName(Name.VIEW_HOME);

        return model;
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(HttpServletRequest request,
            @RequestParam(value = Name.PARAM_ERROR, required = false) String error,
            @RequestParam(value = Name.PARAM_LOGOUT, required = false) String logout) {

        ModelAndView model = new ModelAndView();
        model.setViewName(Name.VIEW_LOGIN);

        if (error != null) {
            addError(model, getErrorMessage(request, "SPRING_SECURITY_LAST_EXCEPTION"));
        }

        if (logout != null) {
            addInfo(model, Message.INFO_USER_LOGOUT);
        }

        return model;
    }
}

在AdminController中,我尝试使用

获取Request
@Controller
@RequestMapping(name = "/admin"}
public class AdminController {

    @RequestMapping(name = "/panel")
    ...

}

但我得到一个例外:

javax.servlet.ServletException: No adapter for handler [com.base.controller.AdminController@3f114da7]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler

个XML:

的web.xml

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/spring-mvc-dispatcher.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/spring-database.xml,
        /WEB-INF/spring/spring-security.xml
    </param-value>
</context-param>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

弹簧MVC-dispatcher.xml

    <mvc:resources mapping="/web_resources/bower_components/**" location="classpath:/web_resources/bower_components/" />
    <mvc:resources mapping="/web_resources/layouts/**" location="classpath:/web_resources/layouts/" />
    <mvc:resources mapping="/web_resources/elements/**" location="classpath:/web_resources/elements/" />

    <context:component-scan base-package="com.base.*" />

    <mvc:annotation-driven />

    <context:annotation-config />

    <import resource="spring-messages.xml" />

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

如果有人要求,我可以分享整个申请表。 在此先感谢和最诚挚的问候。

1 个答案:

答案 0 :(得分:3)

您必须修复您的请求映射,如下所示:

@Controller
@RequestMapping(value = "/admin"}
public class AdminController {

    @RequestMapping(value = "/panel")
    ...

}