SpringMVC【開發Controller】詳解

Java3y發表於2019-03-04

前言

本文主要是講解在Controller中的開發,主要的知識點有如下:

  • 編碼過濾器
  • 使用註解開發
  • 註解@RequestMapping詳解
  • 業務方法接收引數
  • 字串轉日期
  • 重定向和轉發
  • 返回JSON

SpringMVC過濾編碼器

在SpringMVC的控制器中,如果沒有對編碼進行任何的操作,那麼獲取到的中文資料是亂碼!

這裡寫圖片描述

即使我們在handle()方法中,使用request物件設定編碼也不行!原因也非常簡單,我們SpringMVC接收引數是通過控制器中的無參構造方法,再經過handle()方法的object物件來得到具體的引數型別的

Struts2是使用攔截器來自動幫我們完成中文亂碼的問題的。那麼SpringMVC作為一個更加強大的框架,肯定也有對應的方法來幫我們完成中文亂碼問題!

值得注意的是:該過濾編碼器只能解決POST的亂碼問題

我們只需要在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>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

複製程式碼

這裡寫圖片描述


註解開發SpringMVC

我們在快速入門的例子中使用的是XML配置的方式來使用SpringMVC的,SpringMVC也能夠支援註解。【個人非常喜歡註解的方式】

我們在使用Action的時候,要麼繼承著AbstractCommandController類,要麼顯示使用註解Controller介面。當我們使用了註解以後就不用顯示地繼承或實現任何類了

開發流程

使用@Controller這個註解,就表明這是一個SpringMVC的控制器!


@Controller
public  class HelloAction  {
    
}

複製程式碼

當然了,現在Spring是不知道有這麼一個註解的,因此我們需要在配置檔案中配置掃描註解

**值得注意的是:*在配置掃描路徑的時候,後面不要加.

不然掃描不了,我不知道學Struts2還是其他的地方時候,習慣加了.*,於是就搞了很久!

   <!--掃描註解,後面不要加.*-->


    <context:component-scan base-package="zhongfucheng"/>
複製程式碼

在控制器中寫業務方法

@Controller
public class HelloAction {

    /**
     *
     * @RequestMapping 表示只要是/hello.action的請求,就交由該方法處理。當然了.action可以去掉
     * @param model 它和ModelAndView類似,它這個Model就是把資料封裝到request物件中,我們就可以獲取出來
     * @return 返回跳轉的頁面【真實路徑,就不用配置檢視解析器了】
     * @throws Exception
     */
    @RequestMapping(value="/hello.action")

    public String hello(Model model) throws Exception{
        System.out.println("HelloAction::hello()");
        model.addAttribute("message","你好");
        return "/index.jsp";
    }

}


複製程式碼

跳轉到index頁面,首頁得到對應的值。


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  這是我的首頁 <br>
  ${message}
  </body>
</html>
複製程式碼

這裡寫圖片描述


當然了,基於註解和基於XML來開發SpringMVC,都是通過對映器、介面卡和檢視解析器的。 只是對映器、介面卡略有不同。但是都是可以省略的!


    <!-- 基於註解的對映器(可選) -->
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

    <!-- 基於註解的介面卡(可選) -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    <!-- 檢視解析器(可選) -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
複製程式碼

更新:上邊的介面卡和對映器只是Spring3.1版本之前使用的、3.1版本之後現在一般用以下的兩個


對映器:
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping


介面卡:
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter

複製程式碼

當然了,這上面兩個配置也可以使用<mvc:annotation-driven>>替代註解處理器和介面卡的配置。


RequestMapping

@RequestMapping能夠控制請求路徑和請求方式!

一個控制器寫多個業務方法

到目前為止,我們都是一個控制器寫一個業務方法,這肯定是不合理的。我們在Struts2中一個Action就對應多個業務方法了。那麼我們在SpringMVC中又怎麼寫呢???

其實我們可以推理出來,@RequestMapping就是用於配置哪個請求對應哪個業務方法的!


public @interface RequestMapping {
    String[] value() default {};

    RequestMethod[] method() default {};

    String[] params() default {};

    String[] headers() default {};
}

複製程式碼

當我們請求hello.action的時候,處理的業務方法是hello().....當我們請求bye.action的時候,處理的業務方法是bye()


@Controller
public class HelloAction {

    /**
     *
     * @RequestMapping 表示只要是/hello.action的請求,就交由該方法處理。當然了.action可以去掉
     * @param model 它和ModelAndView類似,它這個Model就是把資料封裝到request物件中,我們就可以獲取出來
     * @return 返回跳轉的頁面【真實路徑,就不用配置檢視解析器了】
     * @throws Exception
     */
    @RequestMapping(value="/hello.action")
    public String hello(Model model) throws Exception{
        System.out.println("HelloAction::hello()");
        model.addAttribute("message","你好");
        return "/index.jsp";
    }
    @RequestMapping(value = "/bye.action")
    public String bye(Model model) throws Exception {
        model.addAttribute("message","再見");
        return "/index.jsp";
    }
}

複製程式碼

這裡寫圖片描述

分模組開發

當然了,我們在Struts2常常使用namespace來進行分模組開發,在SpringMVC中我們也可以這樣幹,並且我們又是使用的是@RequestMapping這個註解!

只要把@RequestMapping這個註解寫到類上面去,就代表了分模組。



@Controller
//我們知道,如果是value屬性上的註解,我們可以把value省略掉的
@RequestMapping("/zhongfucheng")
public class HelloAction {

    /**
     * @param model 它和ModelAndView類似,它這個Model就是把資料封裝到request物件中,我們就可以獲取出來
     * @return 返回跳轉的頁面【真實路徑,就不用配置檢視解析器了】
     * @throws Exception
     * @RequestMapping 表示只要是/hello.action的請求,就交由該方法處理。當然了.action可以去掉
     */
    @RequestMapping(value = "/hello.action")
    public String hello(Model model) throws Exception {
        System.out.println("HelloAction::hello()");
        model.addAttribute("message", "你好");
        return "/index.jsp";
    }

    @RequestMapping(value = "/bye.action")
    public String bye(Model model) throws Exception {
        model.addAttribute("message", "再見");
        return "/index.jsp";
    }
}

複製程式碼

那麼我們想要HelloAction該控制器處理我們的請求,訪問的地址要麼是:http://localhost:8080/zhongfucheng/hello.action,或者要麼是http://localhost:8080/zhongfucheng/bye.action


限定某個業務控制方法,只允許GET或POST請求方式訪問

我們如果想要限定某個業務控制方法,只允許GET或POST請求方式訪問。還是通過@RequestMapping來實現。只要設定它的method屬性就行了


    @RequestMapping(value = "/bye.action",method = RequestMethod.POST)
    public String bye(Model model) throws Exception {
        model.addAttribute("message", "再見");
        return "/index.jsp";
    }
複製程式碼

當我把業務方法的請求設定為POST以後,我想要通過GET方式來訪問該業務方法。就行不通了!

這裡寫圖片描述


業務方法寫入傳統web引數

我們的業務方法除了可以寫Model這個引數以外,如果有需要我們還可以寫request,response等傳統Servlet的引數。這是一樣可以使用的....

但是呢,我們並不建議使用傳統的web引數,因為會耦合


@RequestMapping(method=RequestMethod.POST,value="/register")
	public String registerMethod(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		//獲取使用者名稱和薪水
		String username = request.getParameter("username");
		String salary = request.getParameter("salary");
		System.out.println("使用者註冊-->" + username + ":" + salary);
		
		//繫結到session域物件中
		request.getSession().setAttribute("username",username);
		request.getSession().setAttribute("salary",salary);
		
		//重定向/jsp/success.jsp頁面
		//response.sendRedirect(request.getContextPath()+"/jsp/success.jsp");
		
		//轉發/jsp/ok.jsp頁面
		request.getRequestDispatcher("/jsp/ok.jsp").forward(request,response);
		
		//轉發(提倡)
		return "/jsp/success.jsp";
	}
複製程式碼

小細節:如果我們的返回值是返回一個真實路徑,而我們在程式中又使用了轉發或重定向。。。那麼具體跳轉的位置就是按我們程式中跳轉的路徑為準

業務方法收集引數

我們在Struts2中收集web端帶過來的引數是在控制器中定義成員變數,該成員變數的名字與web端帶過來的名稱是要一致的...並且,給出該成員變數的set方法,那麼Struts2的攔截器就會幫我們自動把web端帶過來的引數賦值給我們的成員變數....

那麼在SpringMVC中是怎麼收集引數的呢????我們SpringMVC是不可能跟Struts2一樣定義成員變數的,因為SpringMVC是單例的,而Struts2是多例的。因此SpringMVC是這樣乾的:

  • 業務方法寫上引數
  • 引數的名稱要和web端帶過來的資料名稱要一致

接收普通引數

如果是普通引數的話,我們直接在方法上寫上與web端帶過來名稱相同的引數就行了!


<form action="${pageContext.request.contextPath}/hello.action" method="post">
    <table align="center">
        <tr>
            <td>使用者名稱:</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>編號</td>
            <td><input type="text" name="id"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="提交">
            </td>
        </tr>
    </table>

</form>

複製程式碼

    @RequestMapping(value = "/hello.action")
    public String hello(Model model, String username, int id) throws Exception {

        System.out.println("使用者名稱是:" + username);
        System.out.println("編號是:" + id);

        model.addAttribute("message", "你好");
        return "/index.jsp";
    }

複製程式碼

效果:

這裡寫圖片描述


接收JavaBean

我們處理表單的引數,如果表單帶過來的資料較多,我們都是用JavaBean對其進行封裝的。那麼我們在SpringMVC也是可以這麼做的。

  • 建立Javabean
  • javaBean屬性與表單帶過來的名稱相同
  • 在業務方法上寫上Javabean的名稱

建立JavaBean,javaBean屬性與表單帶過來的名稱相同


public class User {

    private String id;
    private String username;

    public User() {
    }
    public User(String id, String username) {
        this.id = id;
        this.username = username;
    }
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                '}';
    }
}

複製程式碼

在業務方法引數上寫入Javabean


    @RequestMapping(value = "/hello.action")
    public String hello(Model model,User user) throws Exception {

        System.out.println(user);
        model.addAttribute("message", "你好");
        return "/index.jsp";
    }

複製程式碼

這裡寫圖片描述


收集陣列

收集陣列和收集普通的引數是類似的,看了以下的程式碼就懂了。


<form action="${pageContext.request.contextPath}/hello.action" method="post">
    <table align="center">
        <tr>
            <td>使用者名稱:</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>愛好</td>
            <td><input type="checkbox" name="hobby" value="1">籃球</td>
            <td><input type="checkbox" name="hobby" value="2">足球</td>
            <td><input type="checkbox" name="hobby" value="3">排球</td>
            <td><input type="checkbox" name="hobby" value="4">羽毛球</td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="提交">
            </td>
        </tr>
    </table>

</form>
複製程式碼

業務方法獲取引數


    @RequestMapping(value = "/hello.action")
    public String hello(Model model,int[] hobby) throws Exception {


        for (int i : hobby) {
            System.out.println("喜歡運動的編號是:" + i);

        }

        model.addAttribute("message", "你好");
        return "/index.jsp";
    }

複製程式碼

效果:

這裡寫圖片描述

收集List<JavaBean>集合

我們在Spring的業務方法中是不可以用List這樣的引數來接收的,SpringMVC給了我們另一種方案!

我們使用一個JavaBean把集合封裝起來,給出對應的set和get方法。那麼我們在接收引數的時候,接收的是JavaBean


/**
 * 封裝多個Emp的物件 
 * @author AdminTC
 */
public class Bean {
	private List<Emp> empList = new ArrayList<Emp>();
	public Bean(){}
	public List<Emp> getEmpList() {
		return empList;
	}
	public void setEmpList(List<Emp> empList) {
		this.empList = empList;
	}
}

複製程式碼

業務方法接收JavaBean物件


	/**
	 * 批量新增員工
	 */
	@RequestMapping(value="/addAll",method=RequestMethod.POST)
	public String addAll(Model model,Bean bean) throws Exception{
		for(Emp emp:bean.getEmpList()){
			System.out.println(emp.getUsername()+":"+emp.getSalary());
		}
		model.addAttribute("message","批量增加員工成功");
		return "/jsp/ok.jsp";
	}
複製程式碼

在JSP頁面直接寫上empList[下表].


<form action="${pageContext.request.contextPath}/emp/addAll.action" method="POST">
    	<table border="2" align="center">
    		<caption><h2>批量註冊員工</h2></caption>
    		<tr>
    			<td><input type="text" name="empList[0].username" value="哈哈"/></td>
    			<td><input type="text" name="empList[0].salary" value="7000"/></td>
    		</tr>
    		<tr>
    			<td><input type="text" name="empList[1].username" value="呵呵"/></td>
    			<td><input type="text" name="empList[1].salary" value="7500"/></td>
    		</tr>
    		<tr>
    			<td><input type="text" name="empList[2].username" value="班長"/></td>
    			<td><input type="text" name="empList[2].salary" value="8000"/></td>
    		</tr>
    		<tr>
    			<td><input type="text" name="empList[3].username" value="鍵狀哥"/></td>
    			<td><input type="text" name="empList[3].salary" value="8000"/></td>
    		</tr>
    		<tr>
    			<td><input type="text" name="empList[4].username" value="綠同學"/></td>
    			<td><input type="text" name="empList[4].salary" value="9000"/></td>
    		</tr>
    		<tr>
    			<td colspan="2" align="center">
    				<input type="submit" value="批量註冊"/>
    			</td>
    		</tr>
    	</table>
    </form>
複製程式碼

其實這種方法看起來也沒有那麼難理解,我們就是向上封裝了一層【與接收普通的JavaBean類似的】


收集多個模型

我們有可能在JSP頁面上即有User模型的資料要收集,又有Emp模型的資料要收集....並且User模型的屬性和Emp模型的屬性一模一樣....此時我們該怎麼辦呢???

我們也是可以在User模型和Emp模型上向上抽象出一個Bean,該Bean有Emp和User物件

/**
 * 封裝User和Admin的物件
 * @author AdminTC
 */
public class Bean {
	private User user;
	private Admin admin;
	public Bean(){}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
}


複製程式碼

在JSP頁面收集的時候,給出對應的型別就行了。


	<form action="${pageContext.request.contextPath}/person/register.action" method="POST">
		<table border="2" align="center">
			<tr>
				<th>姓名</th>
				<td><input type="text" name="user.username" value="${user.username}"/></td>
			</tr>
			<tr>
				<th>月薪</th>
				<td><input type="text" name="user.salary" value="${user.salary}"></td>
			</tr>
			<tr>
				<th>入職時間</th>
				<td><input 
						type="text" 
						name="user.hiredate" 
						value='<fmt:formatDate value="${user.hiredate}" type="date" dateStyle="default"/>'/></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="submit" value="普通使用者註冊" style="width:111px"/>
				</td>
			</tr>
		</table>	
	</form>	
複製程式碼

字串轉日期型別

我們在Struts2中,如果web端傳過來的字串型別是yyyy-mm-dd hh:MM:ss這種型別的話,那麼Struts2預設是可以自動解析成日期的,如果是別的字串型別的話,Struts2是不能自動解析的。要麼使用自定義轉換器來解析,要麼就自己使用Java程式來解析....

而在SpringMVC中,即使是yyyy-mm-dd hh:MM:ss這種型別SpringMVC也是不能自動幫我們解析的。我們看如下的例子:

JSP傳遞關於日期格式的字串給控制器...

<form action="${pageContext.request.contextPath}/hello.action" method="post">
    <table align="center">
        <tr>
            <td>使用者名稱:</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>出生日期</td>
            <td><input type="text" name="date" value="1996-05-24"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="提交">
            </td>
        </tr>
    </table>

</form>

複製程式碼

User物件定義Date成員變數接收


 public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
複製程式碼

業務方法獲取Date值


    @RequestMapping(value = "/hello.action")
    public String hello(Model model, User user) throws Exception {

        System.out.println(user.getUsername() + "的出生日期是:" + user.getDate());


        model.addAttribute("message", "你好");
        return "/index.jsp";
    }
複製程式碼

結果出問題了,SpringMVC不支援這種型別的引數:

這裡寫圖片描述


現在問題就丟擲來了,那我們要怎麼解決呢????

SpringMVC給出類似於Struts2型別轉換器這麼一個方法給我們使用:如果我們使用的是繼承AbstractCommandController類來進行開發的話,我們就可以重寫initBinder()方法了....

具體的實現是這樣子的:


    @Override
    protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }

複製程式碼

那我們現在用的是註解的方式來進行開發,是沒有重寫方法的。因此我們需要用到的是一個註解,表明我要重寫該方法


    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(
                Date.class,
                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
        
    }
複製程式碼

再次訪問:

這裡寫圖片描述

值得注意的是:如果我們使用的是Oracle插入時間的話,那麼我們在SQL語句就要寫TimeStrap時間戳插入進去,否則就行不通


結果重定向和轉發

我們一般做開發的時候,經常編輯完資料就返回到顯示列表中。我們在Struts2是使用配置檔案進行重定向或轉發的:

這裡寫圖片描述

而我們的SpringMVC就非常簡單了,只要在跳轉前寫上關鍵字就行了!



    public String hello(Model model, User user) throws Exception {

        System.out.println(user.getUsername() + "的出生日期是:" + user.getDate());
        model.addAttribute("message", user.getDate());
        return "redirect:/index.jsp";
    }
複製程式碼

這裡寫圖片描述

以此類推,如果是想要再次請求的話,那麼我們只要寫上對應的請求路徑就行了!


    @RequestMapping(value = "/hello.action")
    public String hello(Model model, User user) throws Exception {

        return "redirect:/bye.action";
    }

    @RequestMapping("/bye.action")
    public String bye() throws Exception {

        System.out.println("我進來了bye方法");
        return "/index.jsp";
    }
複製程式碼

這裡寫圖片描述


返回JSON文字

回顧一下Struts2返回JSON文字是怎麼操作的:

  • 匯入jar包
  • 要返回JSON文字的物件給出get方法
  • 在配置檔案中繼承json-default包
  • result標籤的返回值型別是json

那麼我們在SpringMVC又怎麼操作呢???

匯入兩個JSON開發包

  • jackson-core-asl-1.9.11.jar
  • jackson-mapper-asl-1.9.11.jar

在要返回JSON的業務方法上給上註解:


    @RequestMapping(value = "/hello.action")
    public
    @ResponseBody
    User hello() throws Exception {

        User user = new User("1", "zhongfucheng");
        return user;
    }
複製程式碼

配置JSON介面卡


	
		<!--  
			1)匯入jackson-core-asl-1.9.11.jar和jackson-mapper-asl-1.9.11.jar
			2)在業務方法的返回值和許可權之間使用@ResponseBody註解表示返回值物件需要轉成JSON文字
			3)在spring.xml配置檔案中編寫如下程式碼:
				-->
			<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
				<property name="messageConverters">
						<list>
							<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
						</list>
				</property>
		    </bean>
	
複製程式碼

測試的JSP


	<input type="button" value="Emp轉JSON"/><p>

    <input type="button" value="List<Emp>轉JSON"/><p>

    <input type="button" value="Map<String,Object>轉JSON"/><p>

    <!-- Map<String,Object>轉JSON -->
    <script type="text/javascript">
        $(":button:first").click(function(){
            var url = "${pageContext.request.contextPath}/hello.action";
            var sendData = null;
            $.post(url,sendData,function(backData,textStaut,ajax){
                alert(ajax.responseText);
            });
        });
    </script>

複製程式碼

測試:

這裡寫圖片描述

Map測試:


    @RequestMapping(value = "/hello.action")
    public
    @ResponseBody
    Map hello() throws Exception {

        Map map = new HashMap();

        User user = new User("1", "zhongfucheng");
        User user2 = new User("12", "zhongfucheng2");
        map.put("total", user);
        map.put("rows", user2);


        return map;
    }
複製程式碼

這裡寫圖片描述

更新------------------------------------------------------------------

如果傳遞進來的資料就是JSON格式的話,我們我們需要使用到另外一個註解@RequestBody,將請求的json資料轉成java物件

這裡寫圖片描述

這裡寫圖片描述


總結

  • 使用註解的開發避免了繼承多餘的類,並且非常簡潔高效。
  • 想要中文不亂碼,僅僅設定request的編碼格式是不行的。因為SpringMVC是通過無參的構造器將資料進行封裝的。我們可以使用SpringMVC提供的過濾器來解決中文亂碼問題。
  • RequestMapping可以設定我們具體的訪問路徑,還可以分模組開發。基於這麼兩個原因,我們就可以在一個Action中寫多個業務方法了。
  • RequestMapping還能夠限制該請求方法是GET還是POST。
  • 在我們的業務方法中,還可以使用傳統的request和response等物件,只不過如果不是非要使用的話,最好就別使用了。
  • 對於SpringMVC自己幫我們封裝引數,也是需要使用與request帶過來的名稱是相同的。如果不相同的話,我們需要使用註解來幫我們解決的。
  • 如果是需要封裝成集合,或者封裝多個Bean的話,那麼我們後臺的JavaBean就需要再向上一層封裝,在業務方法上寫上Bean進行了。當然了,在web頁面上要指定對應Bean屬性的屬性
  • 字串轉日期物件用到 @InitBinder註解來重寫方法。
  • 返回JSON物件,我們就需要用到@ResponseBody註解,如果接收JSON資料封裝成JavaBean的話,我們就需要用到@RequestBody註解。隨後在配置檔案上建立對應的bean即可。

如果文章有錯的地方歡迎指正,大家互相交流。習慣在微信看技術文章,想要獲取更多的Java資源的同學,可以關注微信公眾號:Java3y

相關文章