2016-01-22 99 views
0

以下是我的servlet如何设置路由路径Spring框架

<context:component-scan base-package="controllers" /> 

<mvc:annotation-driven/> 

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

我在控制器包不同的控制器。我想,当用户输入

产品/指数

它应该去productControllers指数 GET/POST类型的方法来设置在春季 布线路径一样

如何在spring框架中设置路由映射。

回答

0

添加类级别和方法级别@RequestMapping注解如下

@Controller 
@RequestMapping("/product") 
public class ProductController{ 

    @RequestMapping("/index") 
    public String index() { 
     return "welcome"; 
    } 

    @RequestMapping("/getProducts") 
    public String getProducts() { 
     //your business logic 
     return "getProducts"; 
    } 
} 

然后请求http://localhost:8080/<context-root>/product/index在您的本地环境将返回welcome.jsp页面。

同样http://localhost:8080/<context-root>/product/getProducts将返回getProducts.jsp页面。

如果你有一个更控制器OrderController并在它的方法getOrder,你可以添加类级别的注释@RequestMapping('/order')和方法级别的注释@RequestMapping('/getOrder'),使URL http://localhost:8080/<context-root>/order/getOrder将调用getOrder控制器方法

0

您可以使用方法@RequestMapping("web/service")注释要执行该路径:

@Controller 
public class WelcomeService 
{ 
    @RequestMapping("/welcome") 
    public void welcomeMethod() 
    { 
     // do stuff 
    } 
} 
+0

问什么IM的当用户打开此链接时,应打开welcomemethod ....“site.com/Welcome/welcomemethod”。怎么做 ? –

+0

路径是考虑相对于Web应用程序根 –

+0

实际上,您提供的示例将始终打开welcome方法/ welcome。但是我想要这个/ controllerName/welcome应该打开这个链接。 –