SpringMVC3 Study (ii)--modelandview object

Source: Internet
Author: User

When the controller finishes processing the request, it typically returns the Modelandview object that includes the view name or view object and some model properties to Dispatcherservlet.

Therefore, it is often necessary to construct the Modelandview object in the controller. The Modelandview class provides several overloaded constructors and a number of convenient methods,

Allows you to construct Modelandview objects according to your preferences. These constructors and methods support view names and view objects in a similar way.

When you have only one model property to return, you can specify that property in the constructor to construct the Modelandview object


On the basis of the previous article, just change the login class

Package Com.itmyhome;import Java.util.arraylist;import Java.util.hashmap;import java.util.list;import java.util.Map ; Import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.RequestMapping; Import Org.springframework.web.servlet.ModelAndView; @Controllerpublic class Login {@RequestMapping (value= "Login") Public Modelandview Login () {Modelandview Mav = new Modelandview (); Mav.setviewname ("Welcome");//Return file name Mav.addobject ( "Message", "Hello Kitty");//listlist<string> list = new arraylist<string> (); List.add ("Java"); List.add ("C + + "), List.add (" Oracle "), Mav.addobject (" Booklist ", list);//mapmap<string,string> map = new hashmap<string, String> (), Map.put ("Zhangsan", "Beijing"), Map.put ("Lisi", "Shanghai"), Map.put ("Wangwu", "Shenzhen"), Mav.addobject ("map", map); return MAV;}}

or, for example, the following methods to build your Modelandview object

@RequestMapping (value= "logout") public Modelandview logout () {String message = "Welcome to the next visit!" "; return new Modelandview (" Logout "," message "," message ");

Then change the welcome.jsp output data

Iterating through a collection can use the JSTL expression to introduce a header file into the JSP

<%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>
Jstl.jar and Standard.jar are imported under Lib.

First of all, where can these two jars be found, of course, can be downloaded on the Internet.

In addition, there are below Tomcat, under \webapps\examples\web-inf\lib .

The premise is that you have not deleted some of the useless items below WebApps.


welcome.jsp

<body>   <!--output ordinary characters--   ${message} <br/>   <!--output List--   <p> book listings </p >   <c:foreach items= "${booklist}" var= "Node" ><c:out value= "${node}" ></c:out>   </c: foreach>   <br/>   <br/>      <!--output Map--   <c:foreach items= "${map}" var= "Node" > Name: <c:out value= "${node.key}" ></c:out> Address: <c:out value= "${node.value}" ></c:out><br />   </c:forEach></body>

Results





SpringMVC3 Study (ii)--modelandview object

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.