iT邦幫忙

DAY 14
0

無痛學習SpringMVC與Spring Security系列 第 14

[Controller]使用@PathVariable以及@RequestParam存取網頁特定網址

  • 分享至 

  • xImage
  •  

在一般網頁中,如果網頁提供很多筆資料,如果頁面能夠呈現的資訊有限,則會讓使用者點選任何一筆資料,提供該筆資料更詳細資訊,要存取特定網址路徑,在Spring MVC提供@PathVariable及@RequestParam,@PathVariable在Spring MVC官方文件是稱作URI Template Pattern的Annotation,@RequestParam則是HTTP Request所帶的參數,當然兩者皆是搭配@RequestMapping來存取特定URL的資訊,但其用法及在網頁上超連結的設定不同,廢話不多說直接看 Controller的code:

@Controller
@RequestMapping("/dcn")
public class DCNController {
	
	@Autowired
	private DCNRepository dcnRepository;
	
	@Autowired
	private DCNService dcnService;
	
	....

	@RequestMapping(value="/dcndetail", method=RequestMethod.GET)
	public String getDCNInfoByRequestParam(@RequestParam("dcnNo") String dcnNo, Model model){
		model.addAttribute("dcn", dcnService.findByNo(dcnNo));
		return "dcninfo";
	}
	@RequestMapping(value="/{dcnNo}", method=RequestMethod.GET)
	public String getDCNInfoByPathVariable(@PathVariable("dcnNo") String dcnNo, Model model){
		model.addAttribute("dcn", dcnService.findByNo(dcnNo));
		return "dcninfo";
	}
	
	
}

以這兩段程式碼來看,幾乎沒有什麼差別,差別在於@RequestMapping的URL mapping value,@PathVariable是以變數作為mapping URL,而@RequestParam則是一般的URL,也因為如此,作超連結的時候@RequestParam的網址會是傳統的/dcndetail?dcnNo=xxxx。

DCNRepository的要新增findByNo,對應JPQL及code如下:

@Override
	public DCN findByNo(String dcnNo) {
		// TODO Auto-generated method stub
		Query query=entityManager.createQuery("select d from DCN d where d.no =:no");
		query.setParameter("no", dcnNo);
		DCN dcn=(DCN)query.getSingleResult();
		return dcn;
	}

接著更新DCNList.jsp,將DCN No作超連結,這時候需要用到Spring tag中的url,該tag會依據專案的根目錄作為root路徑,省掉思考相對路徑的問題,僅列出部分含超連結的部份:

@RequestParam超連結的部分
	<c:forEach items="${dcns}" var="dcn" varStatus="counter">
			<td class="text-center">
                        <a 
                        href="  <spring:url value="/dcn/dcndetail?dcnNo=${dcn.no }" /> ">
                       ${dcn.no}
                        </a>
                        </td>
	
	</c:forEach>

另外,新增簡單的dcninfo.jsp,code如下

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${dcn.no } R${dcn.rev }</title>


<div class="grid">
	<div class="row">
			<div class="span1"></div>
			<div class="span5">
				<div class="panel" data-role="panel">
					<div class="panel-header bg-darkBlue fg-white">DCN Detail</div>
					<div class="panel-content">
						<label>DCN No:</label> <label class="text-info"><strong>${dcn.no }</strong></label>
						<label>DCN Rev</label> <label class="text-info"><strong>${dcn.rev }</strong></label>
						<label>Category</label> <label class="text-info"><strong>${dcn.category }</strong></label>
						<label>Tracking Number</label> <label class="text-info"><strong>${dcn.trackNumber }</strong></label>
						<label>Issued Date</label> <label class="text-info"><strong>${dcn.issuedDate }</strong></label>
						<label>Completed Date</label> <label class="text-info"><strong>${dcn.completedDate }</strong></label>
					</div>
				</div>
			</div>
		</div>
</div>

啟動Server,點選任一筆紀錄,畫面如下:

Console log:

22:16:38 [http-bio-8080-exec-3] DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/SpringMVC/dcn/dcndetail]
22:16:38 [http-bio-8080-exec-3] RequestMappingHandlerMapping - Looking up handler method for path /dcn/dcndetail
22:16:38 [http-bio-8080-exec-3] RequestMappingHandlerMapping - Returning handler method [public java.lang.String tw.blogger.springtech.springmvc.controller.DCNController.getDCNInfoByRequestParam(java.lang.String,org.springframework.ui.Model)]
22:16:38 [http-bio-8080-exec-3] DispatcherServlet - Last-Modified value for [/SpringMVC/dcn/dcndetail] is: -1
Hibernate: select dcn0_.prikey as prikey1_0_, dcn0_.category as category2_0_, dcn0_.completedDate as complete3_0_, dcn0_.issuedDate as issuedDa4_0_, dcn0_.no as no5_0_, dcn0_.rev as rev6_0_, dcn0_.trackNumber as trackNum7_0_ from DCN dcn0_ where dcn0_.no=?
22:16:38 [http-bio-8080-exec-3] DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'dcninfo'; URL [/WEB-INF/views/dcninfo.jsp]] in DispatcherServlet with name 'dispatcher'
22:16:38 [http-bio-8080-exec-3] JstlView - Added model object 'dcn' of type [tw.blogger.springtech.springmvc.model.DCN] to request in view with name 'dcninfo'
22:16:38 [http-bio-8080-exec-3] JstlView - Added model object 'org.springframework.validation.BindingResult.dcn' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'dcninfo'
22:16:38 [http-bio-8080-exec-3] JstlView - Forwarding to resource [/WEB-INF/views/dcninfo.jsp] in InternalResourceView 'dcninfo'
22:16:38 [http-bio-8080-exec-3] DispatcherServlet - Successfully completed request





@PathVariable的部分,更新DCNList.jsp中的超連結





    <c:forEach items="${dcns}" var="dcn" varStatus="counter">
            <td class="text-center">
                        <a 
                        href="  <spring:url value="/dcn/${dcn.no }" /> ">
                       ${dcn.no}
                        </a>
                        </td>
    
    </c:forEach>

點選另一筆紀錄,畫面如下

Console log:

22:22:59 [http-bio-8080-exec-7] DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/SpringMVC/dcn/NED-LM1-H-N19805]
22:22:59 [http-bio-8080-exec-7] RequestMappingHandlerMapping - Looking up handler method for path /dcn/NED-LM1-H-N19805
22:22:59 [http-bio-8080-exec-7] RequestMappingHandlerMapping - Returning handler method [public java.lang.String tw.blogger.springtech.springmvc.controller.DCNController.getDCNInfoByPathVariable(java.lang.String,org.springframework.ui.Model)]
22:22:59 [http-bio-8080-exec-7] DispatcherServlet - Last-Modified value for [/SpringMVC/dcn/NED-LM1-H-N19805] is: -1
Hibernate: select dcn0_.prikey as prikey1_0_, dcn0_.category as category2_0_, dcn0_.completedDate as complete3_0_, dcn0_.issuedDate as issuedDa4_0_, dcn0_.no as no5_0_, dcn0_.rev as rev6_0_, dcn0_.trackNumber as trackNum7_0_ from DCN dcn0_ where dcn0_.no=?
22:22:59 [http-bio-8080-exec-7] DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'dcninfo'; URL [/WEB-INF/views/dcninfo.jsp]] in DispatcherServlet with name 'dispatcher'
22:22:59 [http-bio-8080-exec-7] JstlView - Added model object 'dcnNo' of type [java.lang.String] to request in view with name 'dcninfo'
22:22:59 [http-bio-8080-exec-7] JstlView - Added model object 'dcn' of type [tw.blogger.springtech.springmvc.model.DCN] to request in view with name 'dcninfo'
22:22:59 [http-bio-8080-exec-7] JstlView - Added model object 'org.springframework.validation.BindingResult.dcn' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'dcninfo'
22:22:59 [http-bio-8080-exec-7] JstlView - Forwarding to resource [/WEB-INF/views/dcninfo.jsp] in InternalResourceView 'dcninfo'
22:22:59 [http-bio-8080-exec-7] DispatcherServlet - Successfully completed request

明天繼續介紹@MatrixVariable以及multiple @RequestParam


上一篇
[Controller/View]資料直接輸出JSON以及XML格式(II, @ResponseBody/HttpMessageConverter/@RestController)
下一篇
[Controller]@MatrixVariable以及@RequestParam多參數存取特定網頁
系列文
無痛學習SpringMVC與Spring Security31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言