如何在spring框架中设置路径路径

时间:2016-01-22 21:45:15

标签: java spring spring-mvc

以下是我的servlet

<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框架中设置路由映射。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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控制器方法