阅读 81

forword

一、目录结构

web.xml

springmvc.xml

二、forword

@Controller
@RequestMapping("/forword")
public class HelloByForwordController {

	/*
	* 转向 by servlet
	* */
	@RequestMapping(value = "/hellowen",method = RequestMethod.GET)
	public void sayHelloWen(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Hello wen by servlet");
		request.getRequestDispatcher("/pages2/hellowen.jsp").forward(request, response);
	}

	/*
	* 转向 by springmvc
	* */
	@RequestMapping(value = "/byewen")
	public String sayHelloWen1(){
		System.out.println("bye wen by springmvc");
		return "byewen";
	}

}

运行效果:

三、redirect

@Controller
@RequestMapping("/redirect")
public class HelloByRedirectController {

	/*
	* 重定向 by servlet
	* */
	@RequestMapping("/helloemma")
	public void sayHelloEmma(HttpServletResponse response) throws IOException {
		System.out.println("Hello emma");
		response.sendRedirect("/springmvc_demo_war/pages2/helloemma.jsp");
	}

	@RequestMapping("/byeemma")
	public String sayByeEmma(RedirectAttributes attributes) throws IOException {
		System.out.println("bye emma");
		attributes.addAttribute("age", 22);
		return "redirect:/redirect/helloagainemma";
	}

	/*
	* 另一种方法:
	* @RequestMapping("/helloagainemma")
	* public String sayHelloAgainEmma(@RequestParam(required=false, defaultValue="22") Integer age, HttpServletRequest request)
	* */
	@RequestMapping(value = "/helloagainemma", method = RequestMethod.GET)
	public ModelAndView sayHelloAgainEmma(Integer age, HttpServletRequest request) throws IOException {
		//Integer age = request.getParameter("age");
		System.out.println("hello again emma");
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("age", age);
		modelAndView.setViewName("/byeemma");
		return modelAndView;
	}


}

运行效果

请求如果为:
效果:

四、补充

Spring MVC前台使用html页面作为视图,配置静态资源后Controller控制器不起作用的解决办法:

原文:https://www.cnblogs.com/aurora-wen/p/15311954.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐