springmvc之获取参数的方法(必看)
1、导包,四大核心包,一个切面包(AOP),logging,web,springmvc
2、配置文件,核心代码如下:
web.xml
<servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--字符编码的filter一定要放在最前面 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- 配置encoding,告诉我们指定的编码格式 --> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <!-- 解决响应乱码 --> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <!-- 支持rest的filter --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
springmvc.xml
<context:component-scan base-package="com.atguigu"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 视图分析器 --> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
index.jsp: 首页进入
<body> <a href="hello" rel="external nofollow" >hello</a><br/> <a href="handle01?user=123456" rel="external nofollow" >获取请求参数</a><br/> <a href="handle02" rel="external nofollow" >获取请求头</a><br/> <form action="saveBook" method="post"> 图书id<input type="text" name="id"/><br/> 图书name<input type="text" name="name"/><br/> 图书author<input type="text" name="author"/><br/> 图书price<input type="text" name="price"/><br/> 图书sales<input type="text" name="sales"/><br/> 图书stock<input type="text" name="stock"/><br/> <hr/> <!-- 级联属性来封装值 --> 作者name;<input type="text" name="person.name"/><br/> 作者address;<input type="text" name="person.address"/><br/> <input type="submit" value="保存图书"/> </form> <hr/> <h2>给页面携带数据</h2> <a href="output01" rel="external nofollow" >output01</a> </body>
3./WEB-INF/pages 跳转后的内容
1).success.jsp
<body> <h1>成功!</h1> ${msg}===${reMsg} </body>
2).testScope.jsp
<body> <h1>测试数据带在了哪个scope</h1> request:${requestScope.msg }<br /> session:${sessionScope.msg }<br /> application:${applicationScope.msg} </body>
4.src/bean包 Author.java
public class Author { private String name; private String address; Book.java:
public class Book { private int id; private String name; private double price; private int sales; private int stock; private Author person; private String imgPath = "static/img/default.jpg"; private String author;
src/controller 包, HelloController.java: 如果不加,则不能访问
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/hello") //连接地址必须加上"/hello" public String hello(){ return "success"; } }
TestParamController.java
@Controller public class TestParamController { /** * 1、直接给方法的参数位置写上一个和请求带来的参数的名字相同的变量 * 2、这个变量就封装的是带来的参数的值 * user = request.getParameter("user") * 3、如果没有就是null * * @RequestParam("user"):指定获取哪个参数的值 * 1、默认发送请求必须带上这个参数; * 否则:HTTP Status 400 - Required String parameter 'user' is not present * 2、required=false可以设置不是必须的;没带null * 3、defaultValue="未命名"指定没带时的默认值; * user = request.getParameter("user"); */ @RequestMapping("/handle01") public String handle01( @RequestParam(value = "user", required = false, defaultValue = "未命名") String user) { System.out.println("获取的User:" + user); return "success"; } /** * 获取请求头; * request.getHeader("User-Agent") * 注解做了下面这些事情 * @RequestHeader("User-Agent")String userAgent; * userAgent = request.getHeader("User-Agent");*/ @RequestMapping("/handle02") public String handle02( @RequestHeader(value = "User-Agent", required = false, defaultValue = "没有的") String userAgent) { System.out.println("User-Agent:" + userAgent); return "success"; } /** * 获取某个cookie的值; * JSESSIONID=B05C018F82AA1B0BD3845831FADFE49A * @CookieValue("JSESSIONID")String jid * 注解做了下面这些事情 * Cookie[] cookies = request.getCookies(); * for(Cookie c:cookies){ * if(c.getName().equals("JSESSIONID")){ * jid = c.getValue(); * } * }*/ @RequestMapping("/handle03") public String handle03( @CookieValue(value = "JSESSIONID", required = false, defaultValue = "hhhhh") String jid) { System.out.println("jid:" + jid); return "success"; } /*传入POJO;直接帮我们封装页面的值; 方便简单,少写很多代码,实现代码分离,解耦和 * 1、book = new Book(); * 2、把book对象中的每一个属性的值查出来,request.getParameter(属性); * 3、把这个值设置进去 * 注意:因为SpringMVC会进行类型转换,所以提交的数据一定要是合法的,否则400错误*/ @RequestMapping("/saveBook") public String handle04(Book book) { System.out.println("book的值:" + book); return "success"; } @RequestMapping("/handle05") // pringMVC还允许我们在请求参数上使用原生的ServletAPI HttpServletRequest HttpServletResponse // HttpSession public String handle05(HttpSession session, HttpServletRequest request, HttpServletResponse response) { session.setAttribute("msg", "哈哈哈"); request.setAttribute("reqMsg", "嘿嘿嘿"); return "success"; } }
src/dataout/ DataOutPutController.java 给页面携带数据
@Controller //给页面携带数据 public class DataOutPutController { /** * 1、返回值改为ModelAndView(包含模型数据(Model)和要去的页面地址(View)); * 数据放在请求域中; * 2、在请求参数上传入Model、Map、ModelMap都行;给他们里面保存的数据会放在请求域中 * Model、Map、ModelMap最终其实都是再有用BindingAwareModelMap; * 相当于给BindingAwareModelMap中保存数据就是给请求域中保存 * Model Map * || || * || \/ * || ModelMap * \/ \/ * ExtendedModelMap【隐含模型】 extends ModelMap implements Model * \/ * BindingAwareModelMap * @return */ @RequestMapping("/output04") public String output04(ModelMap model){ //视图解析器会对视图名进行拼串 model.addAttribute("msg","output04"); System.out.println(model.getClass()); return "testScope"; } @RequestMapping("/output03") public String output03(Model model){ model.addAttribute("msg", "output03"); System.out.println(model.getClass()); return "testScope"; } @RequestMapping("/output02") public String output02(Map<String,Object>map){ //视图解析器会对视图名进行拼串 map.put("msg", "output02"); System.out.println(map.getClass()); return "testScope"; } @RequestMapping("/output01") public ModelAndView output01(){ //视图解析器会对视图名进行拼串 ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("testScope"); modelAndView.addObject("msg", "output01"); return modelAndView; } }
以上这篇springmvc之获取参数的方法(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)