Java59springMVC
Posted 码农编程录
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java59springMVC相关的知识,希望对你有一定的参考价值。
1.配置文件
//springMVC.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.itheima.controller"></context:component-scan>
<!-- 默认也配置了jackson用于转换json -->
<mvc:annotation-driven>
<!-- <mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="defaultCharset" value="utf-8"></property>
</bean>
</mvc:message-converters>-->
</mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置springMVC的文件解析器 -->
<!-- 配置文件上传解析器,注意,此处id必须为 multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 配置文件上传的最大值 -->
<property name="maxUploadSize" value="1000000"></property>
<!-- 配置上传文件的名字编码格式 -->
<property name="defaultEncoding" value="utf-8"></property>
</bean>
<bean class="com.itheima.exception.MyException"></bean>
<!-- 配置springMVC的拦截器
mvc:interceptors:声明开始配置拦截器
-->
<mvc:interceptors>
<!-- 配置一个具体的拦截器-->
<mvc:interceptor>
<!--配置拦截器需要拦截的路径
/ 表示根目录
/* 表示根目录下的任意目录。但是仅仅局限于根目录下的路径
/** **统配任意多级路径
-->
<mvc:mapping path="/demo14.do"/>
<!-- 将拦截器对象装配到spring容器中 -->
<bean class="com.itheima.intercepors.MyInterceptor1"></bean>
</mvc:interceptor>
<!-- 配置一个具体的拦截器-->
<mvc:interceptor>
<mvc:mapping path="/demo14.do"/>
<!-- 将拦截器对象装配到spring容器中 -->
<bean class="com.itheima.intercepors.MyInterceptor2"></bean>
</mvc:interceptor>
<!-- 配置一个具体的拦截器-->
<mvc:interceptor>
<mvc:mapping path="/demo14.do"/>
<!-- 将拦截器对象装配到spring容器中 -->
<bean class="com.itheima.intercepors.MyInterceptor3"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 配置spring提供的乱码过滤器 -->
<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>
</web-app>
2.前端
//form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--<form action="/hello/show26.do" method="post">
name:<input type="text" name="userList[0].name"/><br/>
age:<input type="text" name="userList[0].age"/><br/>
isMarry:<input type="radio" name="userList[0].isMarry" value="1"/>Y
<input type="radio" name="userList[0].isMarry" value="0"/>N<br>
income:<input type="text" name="userList[0].income"/><br/>
interests:<input type="checkbox" name="userList[0].interests" value="bb"/>basketball
<input type="checkbox" name="userList[0].interests" value="fb"/>football
<input type="checkbox" name="userList[0].interests" value="vb"/>vollyball<br/>
<br>
<br>
name:<input type="text" name="userList[1].name"/><br/>
age:<input type="text" name="userList[1].age"/><br/>
isMarry:<input type="radio" name="userList[1].isMarry" value="1"/>Y
<input type="radio" name="userList[1].isMarry" value="0"/>N<br>
income:<input type="text" name="userList[1].income"/><br/>
interests:<input type="checkbox" name="userList[1].interests" value="bb"/>basketball
<input type="checkbox" name="userList[1].interests" value="fb"/>football
<input type="checkbox" name="userList[1].interests" value="vb"/>vollyball<br/>
<input type="submit" value="提交"/>
</form>-->
<form action="/demo10.do" method="post" enctype="multipart/form-data">
<input type="text" name="username">
<input type="text" name="password">
<input type="file" name="files" multiple="multiple">
<input type="submit">
</form>
<!--multiple多选,selected默认选中-->
<select multiple="multiple">
<option>aaaa</option>
<option selected>bbbb</option>
<option>ccc</option>
</select>
</body>
</html>
//demo.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: hxc
Date: 2019/11/26
Time: 15:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
this is demo jsp
<c:forEach items="${userList}" var="user">
${user.name}::::${user.age}<br/>
</c:forEach>
</body>
</html>
//error.jsp
<%--
Created by IntelliJ IDEA.
User: hxc
Date: 2019/11/26
Time: 16:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
3.controller
package com.itheima.controller;
import com.itheima.domain.User;
import com.itheima.domain.UserVo;
import com.itheima.exception.MyException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Controller
public class MyController {
@RequestMapping("demo1")
public String demo1(){
return "demo";
}
@RequestMapping("demo2")
public String demo2(@RequestParam("ids") List<String> ids){
for(String id:ids){
System.out.println(id);
}
return "demo";
}
@RequestMapping("/hello/show26.do")
public String demo3(UserVo userVo){
for(User user:userVo.getUserList()){
System.out.println(user);
}
return "demo";
}
@RequestMapping("demo4")
public String demo4(Model model){
List<User> userList = new ArrayList<>();
User user1 = new User();
user1.setName("zhangsan1");
user1.setAge(23);
User user2 = new User();
user2.setName("lisi");
user2.setAge(24);
userList.add(user1);
userList.add(user2);
model.addAttribute("userList",userList);
return "demo"; //这行就是jsp页面
}
/**
* springMVC中使用json
* 注意:1.方法上添加 @ResponseBody ,通知springMVC,返回值解析成json响应给前端
* 2.必须引入jackson依赖,因为springMVC底层默认使用的是jackson的工具转换json字符串
* 3.必须在springMVC.xml中配置注解驱动<mvc:annotation-driven></>
*/
@ResponseBody //通知springMVC,返回值解析成json响应给前端
@RequestMapping("demo5.do")
public User demo5(){
User user = new User();
user.setName("zhangsan1");
user.setAge(23);
return user;
}
@ResponseBody //通知springMVC,返回值解析成json响应给前端
@RequestMapping("demo6.do")
public List<User> demo6(){
List<User> userList = new ArrayList<>();
User user1 = new User();
user1.setName("zhangsan1");
user1.setAge(23);
User user2 = new User();
user2.setName("lisi");
user2.setAge(24);
userList.add(user1);
userList.add(user2);
return userList;
}
/**
* @RequestBody : 通知springMVC,使用默认的jackson转换工具,将请求中提交的json格式
* 的字符串参数,转换成java中的user对象。
*/
@ResponseStatus(HttpStatus.OK)//没有返回值
@RequestMapping("demo7")
public void demo7(@RequestBody User user){
System.out.println(user);
}
@ResponseStatus(HttpStatus.OK)//没有返回值
@RequestMapping("demo8")
public void demo8(@RequestBody String user){
System.out.println(user);
}
//上传通过http协议,文件通过字节方式上传,http中是字节,java代码中拿到字节,再把字节转换为对应文件,
//这些操作需要自己去做,操作麻烦,apache的commons-fileupload工具类对请求中的字节转化为文件,更方便。
/**
* 文件上传注意的细节:
* 1.请求的方式必须是post类型
* 2.上传时,文件的参数必须和@RequestParam("key")中的key一致
* 3.上传文件时,数据提交的格式必须是 multipart/form-data类型
* 4.如果上传多个文件,<input type="file" name="file" multiple="multiple">
* multiple="multiple" 表示多选
*/
@RequestMapping("demo9")
@ResponseStatus(HttpStatus.OK)
public void demo9(@RequestParam("file") MultipartFile file){ //获取key=file
String fileName = file.getOriginalFilename(); //获取原文件名
try {
file.transferTo(new File("f://"+fileName));//保存到指定的位置
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping("demo10")
@ResponseStatus(HttpStatus.OK)
public void demo10(@RequestParam("files") MultipartFile[] files) throws Exception{
for(MultipartFile file:files){ //多个文件上传
String fileName = file.getOriginalFilename();
file.transferTo(new File("g://"+fileName));//保存到指定的位置
}
}
@RequestMapping("demo11")
public String demo11(){
return "redirect:demo13.do?type=redirect"; //重定向
}
@RequestMapping("demo12")
public String demo12(HttpServletRequest request)以上是关于Java59springMVC的主要内容,如果未能解决你的问题,请参考以下文章
Spring MVC 3.2 Thymeleaf Ajax 片段