
直接返回字符串:此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转
@RequestMapping("/con")
public String say(){
return "success";
}<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/src/web_jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>最后访问的地址:/src/web_jsp/success.jsp
// 既然SpringMVC会自动回填框架中存在的数据,那么可以直接声明request对象,它一样可以进行回填
@RequestMapping("/test4")
public String test4(HttpServletRequest request){
request.setAttribute("name","赵六");
return "/src/web_jsp/index.jsp";
}
// 设置模型,也就是直接设置数据返回字符串方式也可以进行返回视图
@RequestMapping("/test3")
public String test3(Model model){
model.addAttribute("name","王五");
return "/src/web_jsp/index.jsp";
}
// 当声明数据类是在框架中存在的类时,SpringMVC会自行注入相对应的对象
@RequestMapping("/test2")
public ModelAndView test2(ModelAndView modelAndView){
modelAndView.addObject("name","李四");// 存放进request域中
modelAndView.setViewName("/src/web_jsp/index.jsp");
return modelAndView;
}
// ModelAndView,可以直接返回该对象,该对象内设置的对应属性与值用于返回数据和视图路径
@RequestMapping("/test1")
public ModelAndView test1(){
ModelAndView modelAndView = new ModelAndView();
// 设置视图路径
modelAndView.addObject("name","张三");// 存放进request域中
modelAndView.setViewName("/src/web_jsp/index.jsp");
return modelAndView;
}通过SpringMVC框架注入的response对象,使用response.getWriter().print(“hello world”)回写数据,此时不需要视图跳转,业务方式返回值void。
@RequestMapping("/test5")
public void test4(HttpServletResponse response) throws IOException {
response.getWriter().print("回写数据");
}将需要回写的字符串直接返回,但此时需要通过@responseBody注解告知SpringMVC框架,方法返回的字符串不是跳转,而是直接在http响应体中返回。
@RequestMapping("/test6")
@ResponseBody
public String test4() throws IOException {
return "直接在响应体中返回数据";
}<mvc:annotation-driven></mvc:annotation-driven>// java代码
@RequestMapping("/test10")
@ResponseBody
public void test9(String name,String age) throws IOException {
System.out.println(name);
System.out.println(age);
}<!-- 浏览器地址栏 - 通过get请求向方法发送数据 -->
http://localhost:8080/路径?name=value&age=value<!-- 浏览器地址栏 - 通过get请求向方法发送数据 -->
http://localhost:8080/路径?name=value&age=valuepublic class User{
private String name;
private String age;
}
// java代码
@RequestMapping("配置路径")
@ResponseBody
public void 方法(实体类类型 接收实体类值) throws IOException {
System.out.println(接收实体类值);
}<!-- 浏览器地址栏 - 通过get请求向方法发送数据 -->
http://localhost:8080/路径?arr=111&arr=张三&arr=李四1@RequestMapping("/test12")
@ResponseBody
public void test11(String[] arr) throws IOException {
System.out.println(Arrays.asList(arr));
}<!-- 开放某一路径下的静态资源文件 -->
<mvc:resources mapping="/src/Document/**" location="/src/Document/"></mvc:resources>
<!-- 如果SpringMVC找不到对应的静态资源,那么将交由内部容器(Tomcat)查询对应的静态资源文件 -->
<mvc:default-servlet-handler></mvc:default-servlet-handler><!-- 设置过滤器,该过滤器将所有的文件编码都设置为UTF-8 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>@RequestMapping("/test15")
@ResponseBody
/*
* defaultValue : 默认值
* value : 映射前参数名
* required : 指定参数名是否包括 默认true
* */
public void test14(@RequestParam(value = "data",required = false,defaultValue = "老刘") String name) throws IOException {
System.out.println(name);
}SpringMVC 支持使用原始 ServletAPI 对象作为控制器方法的参数进行注入,常用的对象如下:
@RequestMapping("/test18")
@ResponseBody
public void test17(HttpServletRequest request,HttpServletResponse response) throws IOException {
System.out.println(request);
System.out.println(response);
}
第一步:
第二步:
<!-- 配置文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property> <!-- 设置编码 -->
<property name="maxUploadSize" value="5000000"></property> <!-- 限制文件最大数据 -->
<property name="maxUploadSize" value="5000000"></property> <!-- 限制文件最大数据 -->
</bean>第三步:
html:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<!-- 注意配置enctype,该配置的是 多部分表单形式。详情请看上面的原理图 -->
<!-- 配置文件上传时,请注意请求方式一定是post请求 -->
<form action="${pageContext.request.contextPath}/test21"
enctype="multipart/form-data" method="post">
<div>
<label for="file">文件</label>
<input type="file" name="file" id="file" />
</div>
<div>
<input type="submit" value="提交">
</div>
</form>
</body>
</html>java:
@RequestMapping("/test21")
@ResponseBody
public void test20(MultipartFile file) throws IOException {
// 文件名称,包括了文件后缀名
String originalFilename = file.getOriginalFilename();
// transferTo 方法并不能新建文件夹,该方法只能转移至已存在的文件夹
file.transferTo(new File("F:\\Java\\test\\" + originalFilename));
}步骤一致,方法一致
当jsp页面中的文件标签name值一致,在控制器中可以使用数组方式接收
// 多文件上传
@RequestMapping("/test22")
@ResponseBody
public void test21(MultipartFile[] file) throws IOException {
// 使用循环遍历即可,步骤一致,方法一致
for (MultipartFile data: file) {
String originalFilename = data.getOriginalFilename();
data.transferTo(new File("F:\\Java\\test\\"+originalFilename));
}
}当jsp页面中的文件标签name值不一致,在控制器中就可以声明多个变量名来接收
@RequestMapping("/test21")
@ResponseBody
public void test20(MultipartFile file,MultipartFile file2,MultipartFile file3) throws IOException {
// 文件名称,包括了文件后缀名
String originalFilename = file.getOriginalFilename();
// transferTo 方法并不能新建文件夹,该方法只能转移至已存在的文件夹
file.transferTo(new File("F:\\Java\\test\\" + originalFilename));
System.out.println(originalFilename);
}