springMVC
SpringMVC:前端控制器,映射器,适配器,控制器,试图解析器
前端控制器
前端控制器在web.xml中配置
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springMVC.xml
1
springmvc
/
两种映射器
text
text
两种适配器
控制器
public class MyController implements org.springframework.web.servlet.mvc.Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
String username = req.getParameter("username");
String password = req.getParameter("password");
// req.setAttribute("username", username);
// req.setAttribute("password", password);
ModelAndView view = new ModelAndView();
if("lisi".equals(username)&&"123".equals(password)){
view.setViewName("Hello.jsp");
}
else{
view.setViewName("login.jsp");
}
return view;
}
}
视图解析器
视图解析器的配置前缀后缀
注解适配器和映射器
注解适配器和映射器要一起使用才有效
Controller里使用注解
@Controller
public class TextControler{
@RequestMapping(show1)
public ModelAndView show1(){}
}
cotroller里面返回数据
ModleAndView类型返回值 返回值用一般用view保存使用 view.addObject方法(域对象也可以)
@RequestMapping("show1")
public ModelAndView show1(HttpServletRequest req){
String username = req.getParameter("username");
String password = req.getParameter("password");
ModelAndView view = new ModelAndView();
view.addObject("username", username);
view.addObject("password", password);
view.setViewName("Hello");
return view;
}
String类型返回值返回数据可以域对象,Modle以及ModleMap来保存数据
@RequestMapping("show2")
public String show2(Model model,HttpServletRequest req){
String username = req.getParameter("username");
String password = req.getParameter("password");
model.addAttribute("username", username);
model.addAttribute("password", password);
return "Hello";
}
void类型返回值要用域对象来保存数据
@RequestMapping("show3")
public void show3(HttpServletRequest req,HttpServletResponse resp){
String username = req.getParameter("username");
String password = req.getParameter("password");
req.setAttribute("username", username);
req.setAttribute("password", password);
try {
req.getRequestDispatcher("Hello.jsp").forward(req, resp);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
参数绑定
简单参数绑定 pojo参数绑定,自定义类型
简单参数绑定:传入的数据和jsp中写入request中的数据参数相同,如果想用形参则使用注解@RequestParam("username")是取值 String name是取得别名
Pojo参数绑定:定义一个实体类和jsp中放入的数据类型相同,然后传入实体类可以获得参数
自定义类型: 创建一个转换类实现Converter
自定义转换类:
public class DateConvert implements Converter{
@Override
public Date convert(String source) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
return format.parse(source);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
springMVC中配置
User实体类中配置
private String username;
private String password;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date date;
控制器:
@RequestMapping("show4")
public String show4(Model model,User user){
model.addAttribute("user", user);
return "Hello";
}
}
参数绑定-数组,包装数据类型 list集合
login.jsp:
user类:
private String username;
private String password;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date date;
private List add;
private String[] hobby;
控制器:
@RequestMapping("show5")
public String show5(Model model,User user){
model.addAttribute("user", user);
return "Hello";
}
全局异常处理开发:
1、自定义异常类
2、自定义异常处理器实现 HandlerExceptionResolver接口
3、在springmvc配置文件中配置全局异常控制器
自定义异常类:
public class CustomException extends Exception{
private String msg;
public CustomException(String msg) {
super(msg);
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
自定义异常处理器:
public class ExceptionHandler implements HandlerExceptionResolver{
@Override
public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse resp, Object object,
Exception ex) {
CustomException customException=null;
if (ex instanceof CustomException) {
customException=(CustomException)ex;
}else{
customException=new CustomException("未知错误");
}
ModelAndView view = new ModelAndView();
view.addObject("error", customException);
view.setViewName("error");
return view;
}
}
springMVC的配置
静态资源的释放
在配置文件中进行配置
文件上传的步骤
1、编写配置文件
2、编写控制器
上传单个文件:
public String upload(MultipartFile file,HttpServletRequest req) throws IllegalStateException, IOException{
if(file==null){
return "uploadFile";
}
//取出文件名
String filename = file.getOriginalFilename();
//根据文件的虚拟入境找到真实入境
String path = req.getServletContext().getRealPath("/temp");
File file2 = new File(path);
if (!file2.exists()) {
//创建目录
file2.mkdirs();
}
//将传入的文件按照真实入境和文件名写出来
file.transferTo(new File(file2,filename));
return "success";
}
批量上传文件
@RequestMapping("/upload1")
public String upload1(MultipartFile[] file,HttpServletRequest req) throws IllegalStateException, IOException{
if (file.length==0) {
return "uploadFile";
}
for (MultipartFile multipartFile : file) {
String filename = multipartFile.getOriginalFilename();
String realPath = req.getServletContext().getRealPath("/team");
File file2 = new File(realPath);
if (!file2.exists()) {
file2.mkdirs();
}
multipartFile.transferTo(new File(file2,filename));
}
return "success";
}
3、编写前端表单页面
注意:把form表单的默认的enctype 类型改成 enctype="multipart/form-data"