SPRING MVC3.2 Case Study--spring MVC3 's various URL mappings +jsp view jumps (3) __jsp

Source: Internet
Author: User
Tags prepare

This section focuses on using SPRINGMVC for controller to view, involving code that contains JSP views,

Unlike the largest configuration point in the previous chapter:

The controller @ResponseBody that no view points to is critical code that returns content directly without turning to the JSP view

@Controller public
class Mappingcontroller {

	@RequestMapping ("/mapping/path") is public
	@ResponseBody String Bypath () {return
		' mapped by path! ';
	}
}

Controller pointing to a specific view, no @responsebody, indicating that a JSP view is pointing

@Controller
@RequestMapping ("/views/*") public
class Viewscontroller {

	@RequestMapping (value= "HTML") method=requestmethod.get) Public
	String prepare (model model) {
		model.addattribute ("foo", "Bar");
		Model.addattribute ("Fruit", "apple");
		return "views/html";
	}

Four usage based on view steering:

1. Make a steering view of yourself

	  http://127.0.0.1:8010/views/html---> Corresponding views/html.jsp
	@RequestMapping (value= "HTML", method= requestmethod.get) Public
	String prepare (model model) {
		model.addattribute ("foo", "Bar");
		Model.addattribute ("Fruit", "apple");
		return "views/html";
	}
	

2.spring point to the default view, Org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator the default view to the turn, see log information:

DEBUG Dispatcherservlet Unable to locate requesttoviewnametranslator with Name ' Viewnametranslator ': Using default [Org.s PRINGFRAMEWORK.WEB.SERVLET.VIEW.DEFAULTREQUESTTOVIEWNAMETRANSLATOR@55A58F]

  http://127.0.0.1:8010/views/viewName---> Corresponding views/viewname.jsp
	@RequestMapping (value= "/viewname", method=requestmethod.get) public
	void Usingrequesttoviewnametranslator (model model) {
		Model.addattribute ("foo", "Bar");
		Model.addattribute ("Fruit", "apple");
	}
	

3.URI @PathVariable Acquisition, note that the parameter names must be consistent. Such as:

  http://127.0.0.1:8010/views/pathVariables/bar/apple---> Corresponding views/html.jsp
	@RequestMapping (value=) Pathvariables/{foo}/{fruit} ", Method=requestmethod.get) public
	string Pathvars (@PathVariable string foo, @ Pathvariable String Fruit) {
		//No need to add @PathVariables ' foo ' and ' fruit ' to the model
		//They would be merge D in the model before rendering return
		"views/html";
	}

4. Assign values to JavaBean using the @pathvariable of the URL

Http://127.0.0.1:8010/views/dataBinding/bar/apple---> Corresponding views/databinding.jsp
	@RequestMapping (value=) Databinding/{foo}/{fruit} ", method=requestmethod.get) public
	String dataBinding (@Valid JavaBean JavaBean, Model Model) {
		//JavaBean "foo" and "fruit" properties populated from URI variables return 
		"views/databinding";
	}
public class JavaBean {
	
	@NotNull
	private String foo;

	@NotNull
	private String fruit;
Note that there is a get SET method, space is limited, not in the article
Group: Java EE system Architecture 203431569, before the group, please mark the development of the software industry and work experience.

Size: 70.7 KB View Picture Attachments

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.