iT邦幫忙

DAY 15
0

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

[Controller]@MatrixVariable以及@RequestParam多參數存取特定網頁

  • 分享至 

  • xImage
  •  

接續昨日的介紹,有時候findByxxx會出現多筆資料,如果要取得唯一的資料,就需要額外的參數,@RequestParam比較單純,可以用Map<String, String>來儲存Request變數,多參數的@RequestParam,網址部分各參數以&隔開,如dcndetail?dcnNo=xxx&rev=xx,今天以findByNoAndRev為例,Server端接受no與rev參數,先更新persistence layer的相關classes新增findByNoAndRev方法與實作

DCNRespository.java與DCNService.java新增

DCN findByNoAndRev(String dcnNo, Integer rev);

DCNRespositoryimp.javav新增

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

DCNServiceImp.java新增
@Override
	public DCN findByNoAndRev(String dcnNo, Integer rev) {
		// TODO Auto-generated method stub
		return dcnRepository.findByNoAndRev(dcnNo, rev);
	}

Controller部分略作修改如下

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

	@RequestMapping(value="/dcndetail", method=RequestMethod.GET)
	public String getDCNInfoByRequestParam(@RequestParam Map<String, String> params, Model model){
		String dcnNo=params.get("dcnNo"); //從Map取出dcnNo資訊
		Integer rev=Integer.parseInt(params.get("rev")); //從Map取出rev資訊	
		model.addAttribute("dcn", dcnService.findByNoAndRev(dcnNo, rev));
		return "dcninfo";
	}
	
}

更新DCNlist超連結部分,

.....

<a href="  <spring:url value="/dcn/dcndetail?dcnNo=${dcn.no }&rev=${dcn.rev}" /> " >${dcn.no}</a>

.....

17:18:48 [http-nio-8080-exec-4] DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/SpringMVC/dcn/dcndetail]
17:18:48 [http-nio-8080-exec-4] RequestMappingHandlerMapping - Looking up handler method for path /dcn/dcndetail
17:18:48 [http-nio-8080-exec-4] RequestMappingHandlerMapping - Returning handler method [public java.lang.String tw.blogger.springtech.springmvc.controller.DCNController.getDCNInfoByRequestParam(java.util.Map<java.lang.String, java.lang.String>,org.springframework.ui.Model)]
17:18:48 [http-nio-8080-exec-4] 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=? and dcn0_.rev=?
17:18:48 [http-nio-8080-exec-4] ContentNegotiatingViewResolver - Requested media types are [text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
17:18:48 [http-nio-8080-exec-4] ContentNegotiatingViewResolver - Returning [org.springframework.web.servlet.view.JstlView: name 'dcninfo'; URL [/WEB-INF/views/dcninfo.jsp]] based on requested media type 'text/html'
17:18:48 [http-nio-8080-exec-4] DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'dcninfo'; URL [/WEB-INF/views/dcninfo.jsp]] in DispatcherServlet with name 'dispatcher'
17:18:48 [http-nio-8080-exec-4] JstlView - Added model object 'dcn' of type [tw.blogger.springtech.springmvc.model.DCN] to request in view with name 'dcninfo'
17:18:48 [http-nio-8080-exec-4] JstlView - Added model object 'org.springframework.validation.BindingResult.dcn' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'dcninfo'
17:18:48 [http-nio-8080-exec-4] JstlView - Forwarding to resource [/WEB-INF/views/dcninfo.jsp] in InternalResourceView 'dcninfo'
17:18:48 [http-nio-8080-exec-4] DispatcherServlet - Successfully completed request

@MatrixVariable部分,雖然其他相關的@RequestMapping、@RequestParam、@PathVariable、@ResponseBody等參數預設都是Enable的,但@MatrixVariable預設是disable的,需在dispatcherservletcontext.xml中<mvc: annotation-driven> 加入下列enable-matrix-variable="true"

<mvc:annotation-driven enable-matrix-variables="true"/>

今天的例子是搭配@PathVariable,帶多參數的@MatrixVariable的結合,網址中各參數以";"隔開,如/dcn/xxxxx;rev=x,Controller code修改如下:

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

        .......
	
	@RequestMapping(value="/{dcnNo}", method=RequestMethod.GET)
	public String getDCNInfoByPathVariable(@PathVariable("dcnNo") String dcnNo, 
			@MatrixVariable("rev") Integer rev, Model model){
		model.addAttribute("dcn", dcnService.findByNoAndRev(dcnNo, rev));
		return "dcninfo";
	}
	
}

若要使用@MatrixVariable,則DCNlist.jsp中超連結需更新如下,

.....

<a href="  <spring:url value="/dcn/${dcn.no };rev=${dcn.rev }" /> " >${dcn.no}</a>

.....

經debug是超連結網址打錯,以至於handler找不到對應的method,修正後啟動Server,畫面如下

16:55:48 [http-nio-8080-exec-1] DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/SpringMVC/dcn/NED-LM9-H-N22458;rev=0]
16:55:48 [http-nio-8080-exec-1] RequestMappingHandlerMapping - Looking up handler method for path /dcn/NED-LM9-H-N22458;rev=0
16:55:48 [http-nio-8080-exec-1] RequestMappingHandlerMapping - Returning handler method [public java.lang.String tw.blogger.springtech.springmvc.controller.DCNController.getDCNInfoByMatrixVariable(java.lang.String,java.lang.Integer,org.springframework.ui.Model)]
16:55:48 [http-nio-8080-exec-1] DispatcherServlet - Last-Modified value for [/SpringMVC/dcn/NED-LM9-H-N22458;rev=0] 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=? and dcn0_.rev=?
16:55:48 [http-nio-8080-exec-1] ContentNegotiatingViewResolver - Requested media types are [text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
16:55:48 [http-nio-8080-exec-1] ContentNegotiatingViewResolver - Returning [org.springframework.web.servlet.view.JstlView: name 'dcninfo'; URL [/WEB-INF/views/dcninfo.jsp]] based on requested media type 'text/html'
16:55:48 [http-nio-8080-exec-1] DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'dcninfo'; URL [/WEB-INF/views/dcninfo.jsp]] in DispatcherServlet with name 'dispatcher'
16:55:48 [http-nio-8080-exec-1] JstlView - Added model object 'dcnNo' of type [java.lang.String] to request in view with name 'dcninfo'
16:55:48 [http-nio-8080-exec-1] JstlView - Added model object 'dcn' of type [tw.blogger.springtech.springmvc.model.DCN] to request in view with name 'dcninfo'
16:55:48 [http-nio-8080-exec-1] JstlView - Added model object 'org.springframework.validation.BindingResult.dcn' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'dcninfo'
16:55:48 [http-nio-8080-exec-1] JstlView - Forwarding to resource [/WEB-INF/views/dcninfo.jsp] in InternalResourceView 'dcninfo'
16:55:48 [http-nio-8080-exec-1] DispatcherServlet - Successfully completed request

上一篇
[Controller]使用@PathVariable以及@RequestParam存取網頁特定網址
下一篇
[Controller]使用@ExceptionHandler自行定義錯誤訊息網頁
系列文
無痛學習SpringMVC與Spring Security31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言