SpringMVC接受页面参数和传参到jsp的几种方法

一:spring mvc获取请求参数的几种方法

1.通过@Pathvariable的方法获取参数

package com.oracle.springmvc.control;

import javax.servlet.http.HttpServletRequest;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

public class ParamsController {

	/**Spring向后台Controller传值的四种方法*/
	/**1.使用HttpServletRequest获取*/
	@RequestMapping(value="/login")
	public String GetParam1(HttpServletRequest request){
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		System.out.println(username+password);
		return "index";
	}
	
	/**2.使用@RequestParam获取页面参数*/
	@RequestMapping(value="/login",method=RequestMethod.POST)
	private String addUser(@RequestParam("username") String username,@RequestParam("password") String password){
		System.out.println("----------------username:"+username+"\t password:"+password);
		return "login";
	}
	
	/**3.占位符的形式在url传值,@PathVariable获取参数*/
	@RequestMapping(value="/login/{userid}")
	public String getUser(@PathVariable("userid") String userid){
		System.out.println("请求的userid为:"+userid);
		return "login";
	}
	
	/**4.自动注入bean的映射,但是这种需要新建一个bean类
	 * 
	 * @RequestMapping("/login.do") 
		public String login(User user) 
		{ 
  		syso(user.getName()); 
  		syso(user.getPass()); 
  	*/
	
	/**5.model对象获取*/
	@RequestMapping(value={"/index","/login"},method=RequestMethod.POST)
	public String login(@RequestParam("username") String username,@RequestParam("password") String password,Model model){
		if(username.equals("dabaojian")||(password.equals("123456")))
		{
			System.out.println("login success!");
			/**model对象向前台传参数的bean;前台jsp取值就直接${username}*/
			model.addAttribute("username", username);
			model.addAttribute("password", password);
			return "index";
		}
		else
		{
			System.out.println("login fail");
			return "login";
		}
	}
}



jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
欢迎登陆!
<!-- action里面直接对应的就是在UserController里面的url的value值 -->
<form action="login" method="post">
<input type="text" name="username" value="dabaojian"/>
<input type="password" name="password" value="123456"/>
<input type="submit" value="点击登录"/>
</form>
</body>
</html>



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将后台的数据展示到JSP页面,可以使用SpringMVC的ModelAndView对象。 以下是一个简单的例子: 1. 在Controller中定义方法,查询数据并将其存储在ModelAndView对象中。 ``` @RequestMapping(value = "/showData", method = RequestMethod.GET) public ModelAndView showData() { List<User> userList = userService.getUserList(); ModelAndView modelAndView = new ModelAndView("showData"); modelAndView.addObject("userList", userList); return modelAndView; } ``` 2. 在JSP页面中,使用EL表达式获取存储在ModelAndView对象中的数据并展示。 ``` <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <body> <table> <tr> <th>Id</th> <th>Name</th> <th>Age</th> </tr> <c:forEach var="user" items="${userList}"> <tr> <td>${user.id}</td> <td>${user.name}</td> <td>${user.age}</td> </tr> </c:forEach> </table> </body> </html> ``` 在这个例子中,我们通过Controller中的showData方法获取了一个名为"userList"的List对象,将其存储在ModelAndView对象中,并返回到JSP页面。在JSP页面中,我们使用EL表达式获取"userList"对象,并通过<c:forEach>标签循环展示每个用户的信息。 需要注意的是,我们在JSP页面中使用了JSTL标签库的<c:forEach>标签。如果您还没有在项目中引入JSTL标签库,则需要在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> ``` 同时,需要在JSP页面的头部添加以下标签库声明: ``` <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值