- 获取前端传来数据
- 方式一通过域名传过来的值与处理方法参数一致
- 跳转页面
- 方式一ModelandView
- 方式二使用原生Servlet
- 方式三通过SpringMVC跳转,不需要视图解析器
- 接收参数中文乱码的问题
- SSM集成
@Controller 表示这是一个controller @Component 表示这是一个组件 @Service 表示这是一个service @@Repository 表示这是dao获取前端传来数据 方式一通过域名传过来的值与处理方法参数一致
http://localhost:8080/test3?name=xiaobai
@RequestMapping("/test3")
public void test3(String name){
System.out.println(name);
}
在SpringMVC中可以使用@PathVariable来绑定获取前端传来的数据
@GetMapping("/test/{a}")
public String test(@PathVariable int a,ModelAndView mv){
mv.addObject("msg","测试MVC"+a);
return "test" ;
}
跳转页面
方式一ModelandView
创建ModelandView,根据View,加上视图解析器跳转到指定页面
前缀 + 视图名称 +后缀
前缀
后缀
@GetMapping("/test/{a}")
public ModelAndView test(@PathVariable int a){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","测试MVC"+a);
//设置试图名称
mv.setViewName("test");
return mv ;
方式二使用原生Servlet
@Controller
public class ServletTest{
@RequestMapping("/test1")
public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
rsp.getWriter().println("Hello,Spring BY servlet API");
}
@RequestMapping("/test2")
public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
//重定向
rsp.sendRedirect("/index.jsp");
}
@RequestMapping("/test3")
public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
//转发
req.setAttribute("msg","/result/t3");
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);
}
}
方式三通过SpringMVC跳转,不需要视图解析器
@Controller
public class ResultSpringMVC {
@RequestMapping("/test1")
public String test1(){
//转发
return "WEB-INF/index.jsp";
}
@RequestMapping("/test2")
public String test2(){
//转发二
return "forward:/index.jsp";
}
@RequestMapping("/test3")
public String test3(){
//重定向
return "redirect:/index.jsp";
}
}
接收参数中文乱码的问题
SpringEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
SpringEncodingFilter
/*
SSM集成
首先导入相关pom文件
项目结构
dao层编写
MybatiisConfig配置,这里面啥都没写,
mapper编写
select * from user where id = #{userid} insert into user(id,name,password,perm) values (#{id},#{name},#{psd},#{perm}) update user set perm = #{perm} where id = #{id}
Impl编写
编写Service层的接口和实现类
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserImpl user;//将UserImpl注入进来
public void setUserImpl(UserImpl user) {
this.user = user;
}
public User selectUser(Map map) {
return user.selectUserID(map);
}
}
写好一个类就去bean里面注册,但这里不需要去bean里注册了, 在xml开启
最后编写Controller
@Controller
public class TestController {
@Autowired
private UserServiceImpl userService;
@RequestMapping(value = "/test4/{id}")
public void test3(@PathVariable("id") int id) throws UnsupportedEncodingException {
System.out.println(id);
HashMap map = new HashMap();
map.put("userid", id);
User user = userService.selectUser(map);
System.out.println(user);
}
测试



