Spring+SpringMVC+MyBatis深入學習及搭建(十五)——SpringMVC注解開發(基礎篇)


轉載請注明出處:http://www.cnblogs.com/Joanna-Yan/p/7065294.html 

前面講到:Spring+SpringMVC+MyBatis深入學習及搭建(十四)——SpringMVC和MyBatis整合

本文主要內容:

(1)商品修改功能開發

(2)@RequestMapping

(3)Controller類中方法的返回值

(4)參數綁定

(5)post中文亂碼

(6)SpringMVC和Struts2的區別

1.商品修改功能開發

1.1需求

操作流程:

(1)進入商品查詢列表頁面;

(2)點擊修改,進入商品修改頁面,頁面中顯示了要修改的商品(從數據庫查詢),

    要修改的商品從數據庫查詢,根據商品id(主鍵)查詢商品信息;

(3)在商品修改頁面,修改商品信息,修改后,點擊提交。

1.2開發mapper

mapper:

  根據id查詢商品信息

  根據id更新Items表的數據

不用開發了,使用逆向工程生成的代碼,Spring+SpringMVC+MyBatis深入學習及搭建(十)——MyBatis逆向工程

1.3開發service

接口功能:

  根據id查詢商品信息

  修改商品信息

public interface ItemsService {
    //商品查詢列表
    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
    
    //根據id查詢商品信息
    public ItemsCustom findItemsById(Integer id) throws Exception;
    
    //修改商品信息
    public void updateItems(Integer id,ItemsCustom itemsCustom) throws Exception;
}
public class ItemsServiceImpl implements ItemsService{
    
    @Autowired
    private ItemsMapperCustom itemsMapperCustom;
    @Autowired
    private ItemsMapper itemsMapper;

    @Override
    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
            throws Exception {
        //通過ItemsMapperCustom查詢數據庫
        return itemsMapperCustom.findItemsList(itemsQueryVo);
    }

    @Override
    public ItemsCustom findItemsById(Integer id) throws Exception {
        Items items=itemsMapper.selectByPrimaryKey(id);
        //中間對商品信息進行業務處理
        //....
        //返回Items的擴展類ItemsCustom
        ItemsCustom itemsCustom=new ItemsCustom();
        //將items的屬性值拷貝到itemsCustom
        BeanUtils.copyProperties(items, itemsCustom);
        return itemsCustom;
    }

    @Override
    public void updateItems(Integer id, ItemsCustom itemsCustom)
            throws Exception {
        //添加業務校驗,通常在service接口對關鍵參數進行校驗
        //校驗id是否為空,如果為空拋出異常
        
        //更新商品信息使用updateByPrimaryKeyWithBLOBs根據id更新items表中所有字段,包括大文本類型字段
        //updateByPrimaryKeyWithBLOBs要求必須傳入id
        itemsCustom.setId(id);
        itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
    }
}

1.4開發controller

方法:

  商品信息修改頁面顯示

  商品信息修改提交

@Controller
public class ItemsController {
    
    @Autowired
    private ItemsService itemsService;

    //商品查詢http://localhost:8080/SpringMVC_MyBatis/queryItems.action
    @RequestMapping("/queryItems")
    public ModelAndView queryItems() throws Exception{
        //調用service查找數據庫,查詢商品列表
        List<ItemsCustom> itemsList=itemsService.findItemsList(null);
        
        //返回ModelAndView
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("itemsList", itemsList);
        //指定視圖
//        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        //下邊的路徑,如果在視圖解析器中配置jsp路徑的前綴和jsp路徑的后綴,修改為
        modelAndView.setViewName("items/itemsList");
        return modelAndView;
    }
    
    //商品信息修改頁面顯示
    @RequestMapping("/editItems")
    public ModelAndView editItems() throws Exception{
        //調用service根據商品id查詢商品信息
        ItemsCustom itemsCustom=itemsService.findItemsById(1);
        //返回ModelAndView
        ModelAndView modelAndView=new ModelAndView();
        //將商品信息放到model
        modelAndView.addObject("itemsCustom",itemsCustom);
        //商品修改頁面
        modelAndView.setViewName("items/editItems");
        return modelAndView;
    }
    
    //商品信息修改提交
    @RequestMapping("/editItemsSubmit")
    public ModelAndView editItemsSubmit() throws Exception{
        //調用service更新商品信息,頁面需要將商品信息傳到此方法
        //......
        
        
        //返回ModelAndView
        ModelAndView modelAndView=new ModelAndView();
        //返回一個成功頁面
        modelAndView.setViewName("success");
        return modelAndView;
    }
}

1.5 編寫jsp

itemsList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查詢商品列表</title>
</head>
<body> 
<form action="${pageContext.request.contextPath }/queryItem.action" method="post">
查詢條件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查詢"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
    <td>商品名稱</td>
    <td>商品價格</td>
    <td>生產日期</td>
    <td>商品描述</td>
    <td>操作</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
    <td>${item.name }</td>
    <td>${item.price }</td>
    <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
    <td>${item.detail }</td>
    
    <td><a href="${pageContext.request.contextPath }/editItems.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>

</table>
</form>
</body>

</html>

editItems.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改商品信息</title>

</head>
<body> 
<form id="itemForm" action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="${itemsCustom.id }"/>
修改商品信息:
<table width="100%" border=1>
<tr>
    <td>商品名稱</td>
    <td><input type="text" name="name" value="${itemsCustom.name }"/></td>
</tr>
<tr>
    <td>商品價格</td>
    <td><input type="text" name="price" value="${itemsCustom.price }"/></td>
</tr>
<%-- 
<tr>
    <td>商品生產日期</td>
    <td><input type="text" name="createtime" value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
</tr>
<tr>
    <td>商品圖片</td>
    <td>
        <c:if test="${item.pic !=null}">
            <img src="/pic/${item.pic}" width=100 height=100/>
            <br/>
        </c:if>
        <input type="file"  name="pictureFile"/> 
    </td>
</tr>
 --%>
<tr>
    <td>商品簡介</td>
    <td>
    <textarea rows="3" cols="30" name="detail">${itemsCustom.detail }</textarea>
    </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"/>
</td>
</tr>
</table>

</form>
</body>

</html>

1.6 商品修改調試

訪問地址:http://localhost:8080/SpringMVC_MyBatis/queryItems.action 點擊修改

接下來,學習一些特性:

2. @RequestMapping

通過RequestMapping注解可以定義不同的處理器映射規則。

2.1URL路徑映射

@RequestMapping(value="/item")或@RequestMapping("/item")

value的值是數組,可以將多個url映射到同一個方法.

2.2窄化請求映射

在Controller Class上添加@RequestMapping(url)指定通用請求前綴, 限制此類下的所有方法請求url必須以請求前綴開頭,通過此方法對url進行分類管理。

那么前面jsp中的請求的地址要相應改變

itemsList.jsp中:

editItems.jsp中:

2.3請求方法限定

出於安全性考慮,對http的鏈接進行方法限制。

如果限制請求為post,進行get請求時,則報錯:

3.Controller類中方法的返回值

3.1返回ModelAndView

Controller方法中定義ModelAndView對象並返回,對象中可添加model數據、指定view。

3.2返回字符串

3.2.1邏輯視圖名

controller方法返回字符串可以指定邏輯視圖名,通過視圖解析器解析為物理視圖地址。

真正視圖(jsp路徑)=前綴+邏輯+邏輯視圖名+后綴

    @RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
    public String editItems(Model model) throws Exception{
        //調用service根據商品id查詢商品信息
        ItemsCustom itemsCustom=itemsService.findItemsById(1);

        //通過形參中的model將model數據傳到頁面
        //相當於modelAndView.addObject方法
        model.addAttribute("itemsCustom", itemsCustom);
        return "items/editItems";
    }

3.2.2Redirect重定向

需求:商品修改提交后,重定向到商品查詢列表。

redirect重定向特點:瀏覽器地址欄中的url會變化。修改提交的request數據無法傳到重定向的地址。因為重定向后重新進行request(request無法共享)

    @RequestMapping("/editItemsSubmit")
    public String editItemsSubmit(HttpServletRequest request) throws Exception{
        //調用service更新商品信息,頁面需要將商品信息傳到此方法
        //......
        
        //重定向到商品的查詢列表
        return "redirect:queryItems.action";
    }
}

3.2.3forward頁面轉發

通過forward進行頁面轉發,瀏覽器地址欄url不變,request可以共享。

    @RequestMapping("/editItemsSubmit")
    public String editItemsSubmit(HttpServletRequest request) throws Exception{
        //調用service更新商品信息,頁面需要將商品信息傳到此方法
        //......
        
        //重定向到商品的查詢列表
//        return "redirect:queryItems.action";
        //頁面轉發
        return "forward:queryItems.action";
    }

驗證request是否可以共享:

在editItems.jsp中提交了一個id,在queryItems.action獲取id。

    //商品查詢http://localhost:8080/SpringMVC_MyBatis/items/queryItems.action
    @RequestMapping("/queryItems")
    public ModelAndView queryItems(HttpServletRequest request) throws Exception{
        //測試forward后request是否可以共享
        System.out.println(request.getParameter("id"));
        
        //調用service查找數據庫,查詢商品列表
        List<ItemsCustom> itemsList=itemsService.findItemsList(null);
        
        //返回ModelAndView
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("itemsList", itemsList);
        //指定視圖
//        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        //下邊的路徑,如果在視圖解析器中配置jsp路徑的前綴和jsp路徑的后綴,修改為
        modelAndView.setViewName("items/itemsList");
        return modelAndView;
    }

3.3返回void

在controller方法形參上可以定義request和response,使用request或response指定響應結果:

(1)使用request轉發頁面,如下:

request.getRequestDispatcher("頁面路徑").forward(request, response);

(2)也可以通過response頁面重定向:

response.sendRedirect("url");

(3)也可以通過response指定響應結果,例如響應json數據如下:

response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");

4.參數綁定

struts接收數據時,是在action類的成員變量中定義你需要接收的參數。

4.1spring參數綁定過程

從客戶端請求key/value數據,經過參數綁定,將key/value數據綁定到controller方法的形參上。

springmvc中,接收頁面提交的數據是通過方法形參來接收。而不是在controller類定義成員變量接收!

4.2默認支持的類型

直接在controller方法形參上定義下邊類型的對象,就可以使用這些對象。在參數綁定過程中,如果遇到下邊類型直接進行綁定。

4.2.1HttpServletRequest

通過request對象獲取請求信息。

4.2.2HttpServletResponse

通過response處理響應信息

4.2.3HttpSession

通過session對象得到session中存放的對象。

4.2.4Model/ModelMap

作用:將model數據填充到request域。

ModelMap是Model接口的實現類,通過Model或ModelMap向頁面傳遞數據,如下:

//調用service查詢商品信息
Items item = itemService.findItemById(id);
model.addAttribute("item", item);

頁面通過${item.XXXX}獲取item對象的屬性值。

使用Model和ModelMap的效果一樣,如果直接使用Model,springmvc會實例化ModelMap。

4.3簡單類型

通過@RequestParam對簡單類型的參數進行綁定。

如果不使用@RequestParam,要求request傳入參數名稱和controller方法的形參名稱一致,方可綁定成功。

如果使用@RequestParam,不用限制request傳入參數名稱和controller方法的形參名稱一致。

 通過required屬性指定參數是否必須要傳入,如果設置為true,沒有傳入參數,報下邊錯誤:

4.4 pojo綁定

    @RequestMapping("/editItemsSubmit")
    public String editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom) throws Exception{
        //調用service更新商品信息,頁面需要將商品信息傳到此方法
        itemsService.updateItems(id, itemsCustom);
        
        //重定向到商品的查詢列表
//        return "redirect:queryItems.action";
        //頁面轉發
        return "forward:queryItems.action";
    }

頁面中<input/>的name和controller的pojo形參中的屬性名稱一致,會自動將頁面中的數據綁定到pojo。

頁面定義:

controller的pojo形參的定義:

      

4.4.1簡單pojo

將pojo對象中的屬性名與傳遞進來的屬性名對應,如果傳進來的參數名稱和對象中的屬性名稱一致則將參數值設置在pojo對象中。

頁面定義如下:

<input type="text" name="name"/>
<input type="text" name="price"/>

Controller方法定義如下:

@RequestMapping("/editItemSubmit")
    public String editItemSubmit(Items items)throws Exception{
    System.out.println(items);

請求參數名稱和pojo的屬性名稱一致,會自動將請求參數賦值給pojo的屬性。

4.4.2包裝pojo

如果采用類似struts中對象.屬性的方式命名,需要將pojo對象作為一個包裝對象的屬性,Controller方法中以該包裝對象最為形參。

包裝對象定義如下:

public class QueryVo {
  private Items items;
}

頁面定義:

<input type="text" name="items.name" />
<input type="text" name="items.price" />

Controller方法定義如下:

public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getItems());

4.5自定義參數綁定

需求:

對於controller形參中pojo對象,如果屬性中有日期類型,需要自定義參數綁定。將請求日期參數串轉成日期類型,要轉換的日期類型和pojo中日期屬性的類型保持一致。

所有自定義參數綁定將日期串轉成java.util.Date類型。

需要向處理器適配器中注入自定義的參數綁定組件。

4.5.1自定義日期類型綁定

package joanna.yan.ssm.controller.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
/**
 * 日期轉換器
 * @author Joanna.Yan
 *
 */
public class CustomDateConverter implements Converter<String, Date>{

    @Override
    public Date convert(String source) {
        System.out.println("source="+source);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}

4.5.2配置方式

4.5.2.1配置方式1

classpath下springmvc.xml中加:

<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 自定義參數綁定 -->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!-- 轉換器 -->
        <property name="converters">
            <list>
                <!-- 日期類型轉換 -->
                <bean class="joanna.yan.ssm.controller.converter.CustomDateConverter"/>
            </list>
        </property>
    </bean>    

4.5.2.2配置方式2

    <!--注解適配器 -->
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
         <property name="webBindingInitializer" ref="customBinder"></property> 
    </bean>
    
    <!-- 自定義webBinder -->
    <bean id="customBinder"
        class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
        <property name="conversionService" ref="conversionService" />
    </bean>
    <!-- conversionService -->
    <bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!-- 轉換器 -->
        <property name="converters">
            <list>
                <bean class="joanna.yan.ssm.controller.converter.CustomDateConverter"/>
            </list>
        </property>
    </bean>

4.6集合類

 4.6.1字符串數組

頁面定義如下:

頁面選中多個checkbox向controller方法傳遞:

<input type="checkbox" name="item_id" value="001"/>
<input type="checkbox" name="item_id" value="002"/>
<input type="checkbox" name="item_id" value="003"/>

傳遞到controller方法中的格式是:001,002,003

Controller方法中可以用String[]接收,定義如下:

public String deleteitem(String[] item_id)throws Exception{
        System.out.println(item_id);
}

4.6.2List

List中存放對象,並將定義的List放在包裝類中,controller使用包裝對象接收。

包裝類中定義List對象,並添加get/set方法如下:

Public class QueryVo {
Private List<Items> itemList;//商品列表

  //get/set方法..
}

頁面定義如下:

<tr>
<td><input type="text" name=" itemsList[0].id" value="${item.id}"/></td>
<td><input type="text" name=" itemsList[0].name" value="${item.name }"/></td>
<td><input type="text" name=" itemsList[0].price" value="${item.price}"/></td>
</tr>
<tr>
<td><input type="text" name=" itemsList[1].id" value="${item.id}"/></td>
<td><input type="text" name=" itemsList[1].name" value="${item.name }"/></td>
<td><input type="text" name=" itemsList[1].price" value="${item.price}"/></td>
</tr>

上邊的靜態代碼改為動態jsp代碼如下:

<c:forEach items="${itemsList }" var="item" varStatus="s">
<tr>
    <td><input type="text" name="itemsList[${s.index }].name" value="${item.name }"/></td>
    <td><input type="text" name="itemsList[${s.index }].price" value="${item.price }"/></td>
    .....
    .....
</tr>
</c:forEach>

Contrller方法定義如下:

public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getItemList());
}

4.6.3Map

 在包裝類中定義Map對象,並添加get/set方法,controller使用包裝對象接收。

包裝類中定義Map對象如下:

Public class QueryVo {
private Map<String, Object> itemInfo = new HashMap<String, Object>();
  //get/set方法..
}

頁面定義如下:

<tr>
<td>學生信息:</td>
<td>
姓名:<inputtype="text"name="itemInfo['name']"/>
年齡:<inputtype="text"name="itemInfo['price']"/>
.. .. ..
</td>
</tr>

Contrller方法定義如下:

public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getStudentinfo());
}

5.post中文亂碼

在web.xml中加入:

  <!-- 解決post亂碼 -->
  <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>

以上可以解決post請求亂碼問題。

對於get請求中文參數出現亂碼解決方法有兩個:

修改Tomcat配置文件添加編碼與工程編碼一致,如下:

<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

另外一種方法對參數進行重新編碼:

String userName=new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8");

ISO8859-1是Tomcat默認編碼,需要將Tomcat編碼后的內容按utf-8編碼。

6.SpringMVC和Struts2的區別

(1)SpringMVC的入口是一個Servlet即前端控制器,而Struts2的入口是一個filter過濾器。

(2)SpringMVC是基於方法開發(一個url對應一個方法),請求參數傳遞到方法的形參,可以設計為單例或多例(建議單例),struts2是基於類開發,傳遞參數是通過類的成員變量,只能設計為多例。

SpringMVC將url和Controller方法映射,映射成功后SpringMVC生成一個Handler對象,對象中止包括了一個method。方法執行結束后,形參數據銷毀。

SpringMVC的Controller開發類似service開發。

(3)Strut采用值棧存儲請求和響應的數據,通過OGNL存取數據,SpringMVC通過參數解析器將request請求內容解析,並給方法形參賦值,將數據和視圖封裝成ModelAndView對象,最后又將ModelAndView中的模型數據通過request域傳輸到頁面。jsp視圖解析器默認使用jstl。

(4)經過實際測試,Struts2速度慢在於使用Struts標簽,如果使用Struts建議使用jstl。

如果此文對您有幫助,微信打賞我一下吧~ 

 


免責聲明!

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



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