答:mvc是一种设计模式,它强制性的将程序的结构分为:模型层(Model)、视图层(View)、控制器层(Controller),每一层只做自己的事情,相互之间互相协作,这样的程序结构易于管理、维护方便扩展
模型层(Model)
视图层(View)
控制器层(Controller)
答:它是spring应用中的一个表示层框架,用于与用户交互,它要做的事情就是以前: jsp+sevlet 要完成的任务spring它是基于servlet进行的一层封装,底层核心组件是servlet
1、创建web程序
2、导入jar
spring 4.2
core,beans,context,expression,aop,aspects
增加: web,webmvc
spirng 3.0.2
logging,aop联盟,织入
jsp:
jstl,standard
3、要web.xml文件,配置前端控制,用于拦截所有请求
mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
mvc
*.do
4、编写控制器类,处理请求
注意:以前的控制器类称为: XXXServlet
在springmvc中控制器类的名称是: XXXController
@Controller//表示,当前是一个控制器组件
public class UserController {
@RequestMapping("/first")
public String init(){
System.out.println("------------->进入控制器");
return "show";
}
}
5、编写applicationContext.xml
6、在applicationContext.xml配置如下代码:
配置组件扫描:
配置视图映射器:
注意:在springmvc控制器类的方法,不再区分提交方式:get 与 post 都进入同一个方法
进入
@RequestMapping("/first")
public String init(){
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~1111111~~~~~~~~~");
return "show";
}
@RequestMapping(value = "/second",method = RequestMethod.POST)
public String init2(){
System.out.println("~~~~~~~~~~~~~~~~~~222222222~~~~~~~~~~~~~~");
return "show";
}
如果映射时,路径中有前缀,在页面跳转时,系统默认情况下,会将前缀当作文件夹要解决此问题,只需要返回的字符串前面加上/
/:代表从工程的根目录下找页面
@RequestMapping("/user/first")
public String init(){
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~1111111~~~~~~~~~");
return "/show";
}
@RequestMapping("/user")
@Controller
public class UserController {
@RequestMapping("/fourth")
public String init4(){
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~4444444~~~~~~~~~");
return "/show";
}
}
以前servlet的取值方式是: request.getParameter("名称");
Springmvc中提供了一种处理编码过滤器,只需要配置即可使用
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encoding
/*
restful风格,这是一种编码风格,并不是什么技术,它提倡尽量不要对外暴露业务字段名称
示例:
/**
* 采用最传统的servlet取值方式
* @return
*/
@RequestMapping(value = "/first",method = RequestMethod.POST)
public String one(HttpServletRequest request){
String id = request.getParameter("id");
String name = request.getParameter("name");
String score = request.getParameter("score");
System.out.println(id+"\t"+name+"\t"+score);
return "show";
}
@Autowired
private HttpServletRequest request;
@RequestMapping(value = "/second",method = RequestMethod.POST)
public String two(){
String id = request.getParameter("id");
String name = request.getParameter("name");
String score = request.getParameter("score");
System.out.println(id+"\t"+name+"\t"+score);
return "show";
}
@RequestMapping(value = "/third",method = RequestMethod.POST)
public String three(int id, @RequestParam("name") String name, int score){
System.out.println(id+"\t"+name+"\t"+score);
return "show";
}
@RequestMapping(value = "/fourth",method = RequestMethod.POST)
public String four(Inf f){
System.out.println(f);
return "show";
}
@RequestMapping(value = "/fifth",method = RequestMethod.POST)
public String five(@RequestParam Map map){
System.out.println(map);
return "show";
}
@RequestMapping(value = "/del")
public String del(int id){
System.out.println("要删除的id:"+id);
return "show";
}
@RequestMapping(value = "/del2/{id}/{name}")
public String del2(@PathVariable("id") int id,@PathVariable("name") String name){
System.out.println("要删除的id:"+id+","+name);
return "/show";
}
@RequestMapping("ajax1")
public void show1(Inf f){
System.out.println(f);
}
@@@@默认方式是转发的方式
@RequestMapping(value = "/fourth",method = RequestMethod.POST)
public String four(Inf f){
System.out.println(f);
request.setAttribute("f",f);
return "show";
}
@@@@@重定向
@RequestMapping(value = "/fourth",method = RequestMethod.POST)
public String four(Inf f){
System.out.println(f);
request.setAttribute("f",f);
return "redirect:show.jsp";
}
@@@@@跳转到指定文件夹中的指定页面
@RequestMapping(value = "/third",method = RequestMethod.POST)
public String three(int id, @RequestParam("name") String name, int score){
System.out.println(id+"\t"+name+"\t"+score);
return "jsp/other";
}
产生作用域的几种方式:
示例:
//产生作用域的第一方式,在括号中动态注入
//全局上下文不能在括号中注入
@RequestMapping("/first")
public String first(HttpServletRequest req, HttpSession ses){
req.setAttribute("req","req111111111");
ses.setAttribute("ses","ses222222222222222");
return "show";
}
@Autowired
private HttpServletRequest req;
@Autowired
private HttpSession ses;
@Autowired
private ServletContext cxt;
@RequestMapping("/second")
public String second(){
req.setAttribute("req","req111111111");
ses.setAttribute("ses","ses222222222222222");
cxt.setAttribute("app","app33333333333333333333");
return "show";
}
/**
* Model的存储范围,等同于request
* @param model
* @return
*/
@RequestMapping("/third")
public String third(Model model){
model.addAttribute("msg","hello....");
Map m = new HashMap();
m.put("id",1);
m.put("name","jack");
m.put("score",100);
model.addAllAttributes(m);
return "show";
}
/**
* ModelAndView它的存储范围,等同于request
* @param mv
* @return
*/
@RequestMapping("/fourth")
public ModelAndView fourth(ModelAndView mv){
mv.addObject("msg2","123456");
mv.setViewName("show");
return mv;
}
步骤:
@RequestMapping("/first")
public void first(HttpServletResponse response) throws Exception{
Inf f = new Inf(1,"杰克",90);
//设置响应编码格式
response.setContentType("text/html;charset=utf-8");
//产生PrintWritere用于输出json
PrintWriter out = response.getWriter();
//将对象转换成json格式的字符串
String json = JSON.toJSONString(f);
//输出json
out.write(json);
out.flush();
out.close();
}
这种方式方便的地方:
步骤:
1、导入jackson的jar
2、如果jasckson要将对象、集合转换成json,需要applicationContext.xml文件中,加上一个配置
3、加入jquery
4、在页面中编写代码,以及引用对应的js文件
5、在ajax1.js文件中,编写代码,处理返回的json
$.ajax({
url:"third2.do",
type:"post",
dataType:"json",
success:function (data) {
//
// $.each(data,function (index,k) {
// alert(k.id+" "+k.name+" "+k.score);
// })
// alert(data.id+" "+data.name+" "+data.score);
alert("字符串:" + data.msg)
alert("对象");
var d = data.f;
alert(d.id + " " + d.name + " " + d.score);
alert("集合");
$.each(data.list, function (index, k) {
alert(k.id + " " + k.name + " " + k.score);
})
}
});
6、在控制器类的方法中,编写代码返回json格式的数据
package org.java.web;
import com.alibaba.fastjson.JSON;
import org.java.entity.Inf;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class InfController2 {
@RequestMapping("/first2")
@ResponseBody
public Inf first2(){
return new Inf(1,"AAA",97);
}
@RequestMapping("/second2")
public @ResponseBody List second2(){
List list = new ArrayList<>();
list.add(new Inf(1,"jack",100));
list.add(new Inf(2,"andy",90));
return list;
}
@RequestMapping("/third2")
public @ResponseBody Map second3(){
List list = new ArrayList<>();
list.add(new Inf(1,"jack",100));
list.add(new Inf(2,"andy",90));
Inf f =new Inf(1,"AAA",97);
String msg = "hello";
Map m = new HashMap();
m.put("f",f);
m.put("list",list);
m.put("msg",msg);
return m;
}
}