复习第4点-4.注入不同的对象(集合对象/)
注入集合对象
<%--
Created by IntelliJ IDEA.
User: 19413
Date: 2023/1/13
Time: 15:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/springmvc_war_exploded/userHobby">
用户姓名:<input type="text" name="username"/><br/>
爱好:<br/>
体育<input type="checkbox" value="Sport" name="userlike"/>
音乐<input type="checkbox" value="Music" name="userlike"/>
艺术<input type="checkbox" value="Art" name="userlike"/><br/>
<input type="submit" value="OK"/>
</form>
</body>
</html>
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class DifferentObject {
@RequestMapping("/userHobby")
public ModelAndView addUser(String username,@RequestParam List<String> userlike) { // 集合前面必须要加 @RequestParam
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/index.jsp");
modelAndView.addObject("name", username);
String hobby = "";
for (String h: userlike) {
hobby += h + " ";
}
modelAndView.addObject("hobby", hobby);
return modelAndView;
}
}
本文来自博客园,作者:jsqup,转载请注明原文链接:https://www.cnblogs.com/jsqup/p/17049807.html

浙公网安备 33010602011771号