springmvc 002 几种传参方式

HelloController.java

package com.demo.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/index")
public class HelloController {
//	method=RequestMethod.POST指定当前方法只能有POST访问
	@RequestMapping(value="/hello.do",method=RequestMethod.POST)
	public ModelAndView sayHello(){
		Map<String,Object> model = new HashMap<>();
		model.put("hello", "你好");
		model.put("world", "世界");
		ModelAndView mav= new ModelAndView("hello", model); 
		return mav;
	}
	@RequestMapping(value="/one")
	public String methodOne(Model model){
		model.addAttribute("world","我的");
		return "success";
	}
	@RequestMapping(value="/two.do")
	public String methodTwo(Model model){
		model.addAttribute("word", "我的");
		// 重定向不走视图解析器
		// /表示绝对路径,如果不加/,则表示相对路径
		return "redirect:/success.jsp";
	}
	
	@RequestMapping(value="/three.do")
	public ModelAndView methodThree(){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/success.jsp");
		System.out.println("地址:"+this);
		return mav;
	}
	
	@RequestMapping(value="/four.do")
	public String methodFour(HttpServletRequest req){
		String nameStr = req.getParameter("name");
		System.out.println("nameStr:"+nameStr);
		return "success";
		//http://localhost:8080/ssy/index/four.do?name=zhangsanasdfg
	}
	
	@RequestMapping(value="/five.do")
	public void methodFive(HttpServletResponse resp) throws IOException{
		resp.setCharacterEncoding("utf-8");
		PrintWriter pw = resp.getWriter();
/*		pw.write("<%@ page contentType='text/html; charset='UTF-8' pageEncoding='UTF-8'%>");
		pw.write("<html>");
		pw.write("<body>");
		pw.write("你好,我是write写的字符.");
		pw.write("</body>");
		pw.write("</html>");*/
		pw.write("hello word!");
		//http://localhost:8080/ssy/index/five.do
	}

	@RequestMapping("/six")
	public String methodSix(HttpServletResponse resp, HttpServletRequest req,
			HttpSession session) {
		String jsessionId = session.getId();
		System.out.println("jsessionId:" + jsessionId);
		return "success";
	}
		
}

index.html

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 	<form action="index/hello.do" method="post"> -->
	<form action="index/five.do" method="post">
<!-- 	<form action="index/two.do" method="post">
	<form action="index/three.do" method="post">
	<form action="index/four.do" method="post">
	<form action="index/five.do" method="post">
	<form action="index/six.do" method="post"> -->
		
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${hello }
${world }
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${world }
</body>
</html>

eclipse导出

项目源码上传资源等待审核中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值