2012-05-11 45 views
0

如何将'/'请求映射到控制器?应用程序的上下文名称为reac,我想在上下文发送到服务器时显示登录页面。我有REACController这样的:Spring MVC - 映射/到控制器

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

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

+0

如何你'DispatcherServlet'映射'web.xml'做呢? –

+0

@TomaszNurkiewicz \t \t 弹簧的Servlet \t \t / \t

回答

0

你想保护网站吗?在这种情况下,您最好将Spring Security连接到项目中并让它为您管理URL安全。

但如果你真的想要映射一些着眼于/然后在你的应用环境下,你还可以添加这样的事情

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

这将使用参数化视图控制器

转发/请求登录视图

仍然不确定为什么你会这样做,因为即使你想在没有安全框架的情况下进行登录/安全检查,Filter也是更好的选择。

编辑:这里是如何通过控制器本身

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

    @RequestMapping() 
    public String showMeTheLoginPage() { 
     return "redirect:/login.html"; 
    } 
} 
+0

我尚未使用Spring Security。它的工作方式与您的建议相同,并且这是最初的实施。我试图让它通过Controller实现工作。 –