栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springmvc 002 几种传参方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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("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"%>




Insert title here



	

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


${hello }
${world }

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


${world }

eclipse导出

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/338114.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号