1.搭建环境
在web.xml中配置视图解析器
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 声明mvc配置文件的位置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringConfiguration.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在配置文件中创建解析视图bean
<context:component-scan base-package="com.zhang"/>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
prefix:指明跳转的目标页面所在的文件夹
suffix:指明该文件的后缀
在WEB-INF下新建一个success.jsp,之后的测试就向这个页面跳转
新建一个类,添加@Controller声明该类为一个控制类
@Controller
public class MyController {
@RequestMapping(value = "/hello")
public String hello(){
return "success";
}
}
@RequestMapping为该方法的请求路径
该注解还有其他的一些常用属性
@Controller
public class MyController {
@RequestMapping(value = "/hello",params = {"username","age!=10"})
public String hello(){
return "success";
}
}
params:指定了请求头或请求体中必须要有名字为username和age的参数,并且age不能等于10,例如:
localhost:8080/hello?username=lisi&age=9
@Controller
public class MyController {
@RequestMapping(value = "/hello",params = {"username","age!=10"},method= RequestMethod.POST)
public String hello(String username,String age){
System.out.println(username+"="+age);
return "success";
}
}
method:指定请求类型,该例子请求类型为post,将得到的值赋值给参数username和age
rest风格:
@PathVariable注解:通过占位符的方式给参数赋值
@Controller
public class MyController {
@RequestMapping(value = "/hello/{username}")
public String hello(@PathVariable("username") String name){
System.out.println(name);
return "success";
}
}
@RequestParam:通过普通的url传递参数给参数赋值
@Controller
public class MyController {
@RequestMapping(value = "/hello")
public String hello(@RequestParam("username") String name,@RequestParam("age") Integer age){
System.out.println(name+"==="+age);
return "success";
}
}
该注解还有其他的一些属性:
- require:默认为true,说明url中必须含有该参数。当设置为false,则可以没有
- defaultValue:设置该参数的默认值
@RequestHeader:获取请求头属性的值,请求头里的属性都可以获取,下面以user-agent为例
@RequestMapping(value = "/header")
public String header(@RequestHeader("User-Agent") String ug){
System.out.println(ug);
return "success";
}
@CookieValue:获取指定的cookie的值,该案例获取JSESSIONID的值
@RequestMapping(value = "/cookie")
public String header(@CookieValue("JSESSIONID") String se){
System.out.println(se);
return "success";
}
@RequestBody:获取请求体的内容,get请求不适用,因为只有post请求有请求体
@RequestMapping("/hello")
public String body(@RequestBody String body){
System.out.println(body);
return "success";
}
@ModelAttribute:当请求参数没有值时,可以在该注解下的方法中指定该参数的值,该注解修饰的方法会比@ReuqestMapping的方法先执行
获取原生servlet原生api
@RequestMapping("/hello")
public String body(HttpServletRequest request, HttpServletResponse response, HttpSession session){
return "success";
}
@SessionAttributes:将键值对存入session,只能放在类上
@Controller
@SessionAttributes({"name"})
public class MyController {
@RequestMapping("/hello")
public String body(Model model){
model.addAttribute("name","张三");
return "success";
}
}