1.方式一(视图解析器)
页面: 前缀+viewName+后缀
第一步:视图解析器里配置前缀和后缀
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--<!– 前缀 –>一般来说,都一样-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀-->
<property name="suffix" value=".jsp"/>
</bean>
2.实现类里写viewName
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","hello");
mv.setViewName("test");
return mv;
}
}
setViewName就是文件名
2.方式二(不用视图解析器)
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/c1")
public class ControllerTest2 {
// 转发
@RequestMapping("s1")
public String test1(Model model){
model.addAttribute("msg","hello,方式一!");
return "/index.jsp";
}
// 转发
@RequestMapping("s2")
public String test2(Model model){
model.addAttribute("msg","hello,方式二!");
return "forward:/index.jsp";
}
//重定向
@RequestMapping("s3")
public String test3(Model model){
model.addAttribute("msg","hello,方式三!");
return "redirect:/index.jsp";
}
}
3.从前端获取参数
三种情况:
- 1.前端的参数名称和方法里的参数名称一致的情况
- 2.前端的参数名称和方法里的参数名称不一致的情况
- 3.返回注入对象
import com.wang.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/user")
public class UserController {
// 前端的参数名称和方法里的参数名称一致的情况
@GetMapping("/t1")
public String test1(String name, Model model){
//同名情况,接收前端参数
System.out.println("接收到的前端的参数是:"+name);
//返回给前端
model.addAttribute("msg",name);
return "test";
}
// 前端的参数名称和方法里的参数名称不一致的情况,建议就算一致也应该把注解加上,安全,规范
// 当方法的参数和前端的参数一样时而注解的参数和前端不一致的时候,仍然会报错,所以注解优先级更高一些
@GetMapping("t2")
public String test2(@RequestParam("username") String name,Model model){
System.out.println("接收到的前端的参数是:"+name);
model.addAttribute("msg",name);
return "test";
}
//返回参数需要注入进对象的时候,只要参数名对应就可以注入,不一致赋值为空
@GetMapping("t3")
public String test3(User user,Model model){
System.out.println("接收到的前端的参数是:"+user);
model.addAttribute("msg",user);
return "test";
}
}
4.向前端返回参数
1.ModelAndView方式
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//返回一个模型视图对象
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ControllerTest1");
mv.setViewName("test");
return mv;
}
}
2.ModelMap
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name, ModelMap model){
//封装要显示到视图中的数据
//相当于req.setAttribute("name",name);
model.addAttribute("name",name);
System.out.println(name);
return "hello";
}
3.Model方式
@RequestMapping("/ct2/hello")
public String hello(@RequestParam("username") String name, Model model){
//封装要显示到视图中的数据
//相当于req.setAttribute("name",name);
model.addAttribute("msg",name);
System.out.println(name);
return "test";
}
4.乱码问题
在web.xml中配置过滤器
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>