入门注解@PathVariable

siye@r480:~/svlution/workspace/springmvc4322$ tree src/
src/
├── main
│   ├── java
│   │   ├── log4j.properties
│   │   └── ocn
│   │       └── site
│   │           └── springmvc
│   │               └── controller
│   │                   └── Manicontroller.java
│   ├── resources
│   └── webapp
│       └── WEB-INF
│           └── web.xml
└── test
    ├── java
    │   └── ocn
    │       └── site
    │           └── springmvc
    │               └── controller
    │                   └── Runtest.java
    └── resources
        └── config
            └── application.xml

17 directories, 5 files
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.22.RELEASE</version>
    <scope>test</scope>
</dependency>
package ocn.site.springmvc.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class Manicontroller {

	private final Logger logger = Logger.getLogger(this.getClass());

	// 路径变量的使用方式有多种搭配的方式 比如/test1/{var1}/test2/{var2}
	// 若是注册了 HiddenHttpMethodFilter 过滤器 可简单的实现RESTful风格的功能
	// 该注解将路径中的变量绑定到请求方法的参数上
	// 在url中 路径变量的声明使用括号 {} 来完成
	@GetMapping("/test/{var}")
	public void handler(@PathVariable String var) {
		logger.info(var);
	}

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="ocn.site.springmvc.controller"></context:component-scan>
	<mvc:annotation-driven></mvc:annotation-driven>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>
package ocn.site.springmvc.controller;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:config/application.xml")
public class Runtest {

	private @Autowired WebApplicationContext wac;

	@Test
	public void run() throws Exception {
		MockMvc build = MockMvcBuilders.webAppContextSetup(this.wac).build();
		MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/test/path");
		ResultActions ra = build.perform(requestBuilder);
		ra.andExpect(MockMvcResultMatchers.status().isOk());
	}

}
19-09-05 14:36:50 org.springframework.test.context.web.WebTestContextBootstrapper  =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-05 14:36:50 org.springframework.test.context.web.WebTestContextBootstrapper  =====>>> Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@6d78f375, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@50c87b21, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5f375618, org.springframework.test.context.support.DirtiesContextTestExecutionListener@1810399e]
19-09-05 14:36:50 org.springframework.beans.factory.xml.XmlBeanDefinitionReader  =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-05 14:36:51 org.springframework.web.context.support.GenericWebApplicationContext  =====>>> Refreshing org.springframework.web.context.support.GenericWebApplicationContext@6eebc39e: startup date [Thu Sep 05 14:36:51 CST 2019]; root of context hierarchy
19-09-05 14:36:51 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping  =====>>> Mapped "{[/test/{var}],methods=[GET]}" onto public void ocn.site.springmvc.controller.Manicontroller.handler(java.lang.String)
19-09-05 14:36:51 org.hibernate.validator.internal.util.Version  =====>>> HV000001: Hibernate Validator 5.2.0.Final
19-09-05 14:36:51 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter  =====>>> Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@6eebc39e: startup date [Thu Sep 05 14:36:51 CST 2019]; root of context hierarchy
19-09-05 14:36:51 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter  =====>>> Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@6eebc39e: startup date [Thu Sep 05 14:36:51 CST 2019]; root of context hierarchy
19-09-05 14:36:52 org.springframework.mock.web.MockServletContext  =====>>> Initializing Spring FrameworkServlet ''
19-09-05 14:36:52 org.springframework.test.web.servlet.TestDispatcherServlet  =====>>> FrameworkServlet '': initialization started
19-09-05 14:36:52 org.springframework.test.web.servlet.TestDispatcherServlet  =====>>> FrameworkServlet '': initialization completed in 22 ms
19-09-05 14:36:52 ocn.site.springmvc.controller.Manicontroller  =====>>> path
19-09-05 14:36:52 org.springframework.web.context.support.GenericWebApplicationContext  =====>>> Closing org.springframework.web.context.support.GenericWebApplicationContext@6eebc39e: startup date [Thu Sep 05 14:36:51 CST 2019]; root of context hierarchy
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值