Arturo
Arturo

Reputation: 261

Spring mvc controller handling all request and should not do that

I have two controllers:

UserController and AdminController.

The AdminController is handling all requests that have not created. Also handle the request to access to my resources.

Also I'm using spring-security and maven to compile all.

Examples:

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.

If I remove the AdminController and create a new controller with name ExamplefasjkasController it happens the same.

Code:

Controllers:

@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;
    }
}

In the AdminController I try to get Request with

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

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

}

But I get an exception:

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

XMLs:

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>

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

I could share the entire application if someone request. Thanks in advance and best regards.

Upvotes: 1

Views: 1368

Answers (1)

Sezin Karli
Sezin Karli

Reputation: 2525

You have to fix your request mapping as below:

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

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

}

Upvotes: 3

Related Questions