0

如何将“/”请求映射到控制器?应用程序的上下文名称是 reac,我想在上下文发送到服务器时调出登录页面。我有这样的 REACController:

@Controller
public class REACController {
@RequestMapping(value="/", method=RequestMethod.GET)
    public String onLaunch() {
        return "LoginPage";
    }
}

我面临着可怕的 404 错误,“资源 /reac/ 不可用”。通过 Controller 类处理对“/”的请求的正确方法是什么?

4

1 回答 1

0

Are you trying to secure the site? In which case you might be better off hooking up Spring Security into the project and have it manage the URL security for you.

But if you really want to map some view to the / then in your application context you can also add something like this

<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 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.1.xsd     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<!--facilitates mapping of Dispatcher Servlet to /-->
<mvc:default-servlet-handler/>
<mvc:view-controller path="/" view-name="login"/>
</beans>

this will forward the / requests to the login view using parameterizable view controller

Still unsure of the reason why you would do something like this as Filter would be a better option even if you were wanting to do login/security check without Security frameworks.

Edit: here is how you can do it through controller itself

 @RequestMapping("/")
 @Controller
 public class MyController {

     @RequestMapping()
     public String showMeTheLoginPage() {
         return "redirect:/login.html";
     }
 }
于 2012-05-11T09:52:13.667 回答