1. 程式人生 > >SpingMVC_註解式開發_接收請求參數

SpingMVC_註解式開發_接收請求參數

style ng- rtu spl char enc b16 oid b-

一、逐個接收

技術分享圖片

技術分享圖片
 1 import org.springframework.stereotype.Controller;
 2 import org.springframework.web.bind.annotation.RequestMapping;
 3 import org.springframework.web.servlet.ModelAndView;
 4 
 5 @Controller // 表示當前類是一個處理器
 6 @RequestMapping("/test")
 7 public class MyController {
 8 
 9     @RequestMapping("/register.do")
10 public ModelAndView doRegister(String name, int age) { 11 // TODO Auto-generated method stub 12 13 System.out.println("name=" + name); 14 System.out.println("age=" + age); 15 16 ModelAndView mv = new ModelAndView(); 17 mv.addObject("name", name);
18 mv.addObject("age", age); 19 mv.setViewName("/WEB-INF/jsp/welcome.jsp"); 20 return mv; 21 } 22 23 }
MyController 技術分享圖片
1 <!--   註冊組件掃描器 -->
2    <context:component-scan base-package="com.jmu.handlers"></context:component-scan>
springmvc.xml 技術分享圖片
 1 <%@ page language="java" import
="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <%@page isELIgnored="false" %> 4 <html> 5 <head> 6 <title>My JSP ‘index.jsp‘ starting page</title> 7 </head> 8 9 <body> 10 <form action="${pageContext.request.contextPath}/test/register.do" 11 method="post"> 12 姓名:<input type="text" name="name"> <br> 13 年齡:<input type="text" name="age"> <br> 14 <input type="submit" value="註冊"> 15 </form> 16 </body> 17 </html>
index.jsp 技術分享圖片
1 <body>
2     name=${name}<br>
3     age=${age}<br>
4   </body>
welcome.jsp

技術分享圖片技術分享圖片

二、解決中文亂碼問題

在CharacterEncodingFilter中

技術分享圖片

在web.xml中添加

 <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <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>

技術分享圖片技術分享圖片

三、校正請求參數名

技術分享圖片

技術分享圖片

四、以對象形式整體接收

技術分享圖片

技術分享圖片
 1 public class Student {
 2     private String name;
 3     private int age;
 4 
 5     public String getName() {
 6         return name;
 7     }
 8 
 9     public void setName(String name) {
10         this.name = name;
11     }
12 
13     public int getAge() {
14         return age;
15     }
16 
17     public void setAge(int age) {
18         this.age = age;
19     }
20 
21     @Override
22     public String toString() {
23         return "Student [name=" + name + ", age=" + age + "]";
24     }
25 
26 }
Student 技術分享圖片
 1 import org.springframework.stereotype.Controller;
 2 import org.springframework.web.bind.annotation.RequestMapping;
 3 import org.springframework.web.servlet.ModelAndView;
 4 
 5 import com.jmu.beans.Student;
 6 
 7 @Controller
 8 @RequestMapping("/test")
 9 public class MyController {
10 
11     @RequestMapping("/register.do")
12     public ModelAndView doRegister(Student student) {
13         System.out.println("name=" + student.getName());
14         System.out.println("age=" + student.getAge());
15 
16         ModelAndView mv = new ModelAndView();
17         mv.addObject("student", student);
18         mv.setViewName("/WEB-INF/jsp/welcome.jsp");
19         return mv;
20 
21     }
22 }
MyController 技術分享圖片
 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3         xmlns:context="http://www.springframework.org/schema/context"
 4         xmlns:aop="http://www.springframework.org/schema/aop"
 5         xmlns:tx="http://www.springframework.org/schema/tx"
 6         xmlns:mvc="http://www.springframework.org/schema/mvc"
 7         xsi:schemaLocation="http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context.xsd
11         http://www.springframework.org/schema/aop
12         http://www.springframework.org/schema/aop/spring-aop.xsd
13         http://www.springframework.org/schema/tx
14         http://www.springframework.org/schema/tx/spring-tx.xsd
15         http://www.springframework.org/schema/mvc
16         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
17  <!--   註冊組件掃描器 -->
18    <context:component-scan base-package="com.jmu.handlers"></context:component-scan>
19 </beans>
springmvc.xml 技術分享圖片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>02-springmvc-Object</display-name>
 4    <filter>
 5     <filter-name>CharacterEncodingFilter</filter-name>
 6     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 7     <init-param>
 8       <param-name>encoding</param-name>
 9       <param-value>UTF-8</param-value>
10     </init-param>
11     <init-param>
12       <param-name>forceEncoding</param-name>
13       <param-value>true</param-value>
14     </init-param>
15   </filter>
16   <filter-mapping>
17   <filter-name>CharacterEncodingFilter</filter-name>
18   <url-pattern>/*</url-pattern>
19   </filter-mapping>
20   <servlet>
21     <servlet-name>springMVC</servlet-name>
22     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
23     <init-param>
24       <param-name>contextConfigLocation</param-name>
25       <param-value>classpath:springmvc.xml</param-value>
26     </init-param>
27     <load-on-startup>1</load-on-startup>
28   </servlet>
29   <servlet-mapping>
30     <servlet-name>springMVC</servlet-name>
31     <url-pattern>*.do</url-pattern>
32   </servlet-mapping>
33   <welcome-file-list>
34     <welcome-file>index.jsp</welcome-file>
35   </welcome-file-list>
36 </web-app>
web.xml 技術分享圖片
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3     String path = request.getContextPath();
 4     String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
 5             + path + "/";
 6 %>
 7 <%@page isELIgnored="false"%>
 8 
 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11 <head>
12 <base href="<%=basePath%>">
13 
14 <title>My JSP ‘index.jsp‘ starting page</title>
15 </head>
16 
17 <body>
18     <form action="${pageContext.request.contextPath }/test/register.do"
19         method="post">
20         姓名:<input type="text" name="name" /><br> 年 齡:<input type="text"
21             name="age" /><br> <input type="submit" value="註冊" />
22     </form>
23 </body>
24 </html>
index.jsp 技術分享圖片
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 <%@page isELIgnored="false" %>
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>welcome  page</title>
13     
14   </head>
15   
16   <body>
17    student=${student} <br>
18   </body>
19 </html>
welcome

技術分享圖片技術分享圖片

五、域屬性參數的接收

域屬性參數,即對象屬性,當請求參數中的數據為某類對象域屬性的屬性值時,要求請求參數名為“域屬性名.屬性”。

技術分享圖片
 1 public class School {
 2     private String sname;
 3     private String address;
 4 
 5     public String getSname() {
 6         return sname;
 7     }
 8 
 9     public void setSname(String sname) {
10         this.sname = sname;
11     }
12 
13     public String getAddress() {
14         return address;
15     }
16 
17     public void setAddress(String address) {
18         this.address = address;
19     }
20 
21     @Override
22     public String toString() {
23         return "School [sname=" + sname + ", address=" + address + "]";
24     }
25 
26 
27 
28 }
School

技術分享圖片技術分享圖片

六、路徑變量

技術分享圖片

技術分享圖片

SpingMVC_註解式開發_接收請求參數