实体类:
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置springMVC需要扫描的包 --> <context:component-scan base-package="com.xiaobiti.controller"/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="WEB-INF/pages"/> <property name="suffix" value=".jsp"/> </bean> </beans>
UserController.java
package com.xiaobiti.controller; import com.xiaobiti.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; @Controller public class UserController { //方法一 @RequestMapping("/getId") //传入参数为请求对象 public void getId(HttpServletRequest request){ String id = request.getParameter("id"); System.out.println(id); } //方法二 @RequestMapping("/getUser") //传入参数直接对应get请求中发送的请求参数 public void getUsernameAndId(String userName,String id){ System.out.println("userName=" + userName + "," + "id=" + id); } //方法三 @RequestMapping("/getUser2") //传入参数是将get请求中发送的请求参数封装成一个对象 public void getUser(User user){ System.out.println(user); } }
跳转测试页面index.jsp代码:
<html> <body> <h2>Hello World!</h2> <a href="firstController">Link</a><br> <a href="getId?id=My_id">getId</a><br> <a href="getUser?userName=abc&id=2">getUser</a><br> <a href="getUser2?username=abc&password=123456">getUser2</a><br> </body> </html>
小小实例:
实体类:
ProductController.java
package com.xiaobiti.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import java.util.List; @Controller public class ProductController { /** * 获得商品列表 */ // @RequestMapping("/getPros") // //传入参数对应请求参数中的全部proIds的值的一个数组 // public void getPros(String[] proIds){ // for (String id:proIds) { // System.out.println(id); // } // } @RequestMapping("/getPros") //传入参数对应请求参数中的全部proIds的值的一个集合 public void getPros(@RequestParam("proIds")List<String> list){ for (String id:list) { System.out.println(id); } } }
商品列表页面product.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 项目路径:${pageContext.request.contextPath} <form action="${pageContext.request.contextPath}/getPros"method="post"> <table width="220px" border="1"> <tr><td>选择</td><td>商品名称</td></tr> <tr> <!-- 这里只展示了一个商品 --> <td><input name="proIds" value="1" type="checkbox"></td> <td>Java基础</td> </tr> <tr> <!-- 这里只展示了一个商品 --> <td><input name="proIds" value="2" type="checkbox"></td> <td>JavaWeb</td> </tr> <tr> <!-- 这里只展示了一个商品 --> <td><input name="proIds" value="3" type="checkbox"></td> <td>SSM框架实战</td> </tr> </table> <input type="submit" value="提交商品"/> </form> </body> </html>
标签:RequestMapping,Spring,绑定,id,MVC,参数,import,public,String From: https://www.cnblogs.com/xiaobiti/p/17306201.html