SpringMVC【開發Controller】詳解


前言

本文主要是講解在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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM