Spring MVC (advanced parameters, List type binding, use of @ RequestMapping annotation, qualification of request method, and method return value)

Keywords: JSP xml Java encoding

Advanced parameter binding

1. Parameter binding of array type

Complete parameter transfer based on bulk deletion

@RequestMapping("queryItem")
	public String queryItem(QueryVo vo, Integer[] ids) {
		System.out.println(vo);
		//Output the array passed in
		if (ids != null && ids.length > 0) {
			for (Integer id : ids) {
				System.out.println("The list of incoming products is as follows:" + id);
			}
		}
		return "itemList";
2. Binding of List type

Complete parameter transfer based on batch modification (controller modification)

@RequestMapping("queryItem")
public String queryItem(QueryVo vo,Integer[] ids){
	System.out.println(vo);
	//Output the array passed in
	if(ids != null && ids.length > 0){
		for(Integer id : ids){
			System.out.println("The parameters passed in are"+id);
		}
	}
}

(POJO modification)

public class QueryVo{
	private Item item;

	//Show me the List parameter passing
	private List<Item> items;
	
	public List<Item> getItems(){
		return items
	}
}

Write a DateConvert class

public class DateConvert implements Converter<String,Date>{
	
	@Override
	public Date convert(String source){
		Date result = null;
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			result = sdf.parse(source);
		}catch(ParseException e){
			e.printStackTrace();
		}
		return result;
	}
}

Configure the date converter in springmvc.xml

<!-- Use custom converter -->
<mvc:annotation-driven conversion-service="MyConvert"

<!-- Define converter -->
<bean id="MyConvert" class="org.springframework.format.support.FormattingConversionServiceFactory" >
	<property name="converters">
		<set>
			<bean class="com.java.springmvc.util.DateConvert"/>
		</set>
	</property>
</bean>

@Use of RequestMapping annotation

Subdirectory Management (take any example)
@RequestMapping("Item")
public class Controller{
	@RequestMapping("queryItem")
	public String queryItem(QueryVo vo,Integer[] ids){
	System.out.println(vo);
	//Output the array passed in
	if(ids != null && ids.length > 0){
		for(Integer id : ids){
			System.out.println("The parameters passed in are"+id);
		}
	}
}
}

At this time, the browser input address is not localhost:8080/queryItem, but also add an Item before queryItem, such as localhost:8080/Item/queryItem

Qualified request method
@RequestMapping(value="queryItem",method=(RequestMethod.POST,method=(RequestMethod.GET))

Return value

	@RequestMapping("queryVoid")
	public void queryVoid(HttpServletRequest request,HttpServletResponse response) throws Exception{
//		Request in response to customer request
//		request.setAttribute("msg", "this is the message of the request response");
//		request.getRequestDispatcher("/WEB-INF/jsp/demo.jsp").forward(request,response);
		//Response in response to user request
		//response.sendRedirect("/hello.action");
		
		//Character encoding of facility response
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter printWriter = response.getWriter();
		printWriter.println("This is response Print messages for");
	}
	@RequestMapping("demo")
	public String name(String msg,Model model) throws Exception{
		model.addAttribute("msg", msg);
		return "redirect:hello.action";
		
	}

Posted by Jove on Tue, 12 Nov 2019 11:39:17 -0800