在Spring MVC中,模型工作于一個(gè)包含應(yīng)用程序數(shù)據(jù)的容器。在這里,數(shù)據(jù)可以采用任何形式,例如對象,字符串,數(shù)據(jù)庫中的信息等。
需要將 Model 接口放置在計(jì)算機(jī)的控制器部分中。應(yīng)用。 HttpServletRequest 對象讀取用戶提供的信息,并將其傳遞給 Model 接口?,F(xiàn)在,視圖頁面可輕松訪問模型零件中的數(shù)據(jù)。
方法 | 說明 |
Model addAllAttributes(Collection <?> arg) | 它將提供的集合中的所有屬性添加到此Map中。 |
Model addAllAttributes(Map <String,?> arg) | 它將提供的Map中的所有屬性添加到此Map中。 |
Model addAllAttribute(Object arg) | 它將使用生成的名稱將提供的屬性添加到此Map。 |
Model addAllAttribute(String arg0, Object arg1) | 它將屬性與提供的名稱綁定。 |
Map<String, Object> asMap() | 它將當(dāng)前模型屬性集作為Map返回。 |
Model mergeAttributes(Map<String,?> arg) | 它將添加的Map中的所有屬性添加到此Map中,同名的現(xiàn)有對象優(yōu)先。 |
boolean containsAttribute(String arg) | 它指示此模型是否包含給定名稱的屬性 |
我們創(chuàng)建一個(gè)包含用戶名和密碼的登錄頁面。在這里,我們使用特定的值來驗(yàn)證密碼。
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.1.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency>
在這里,我們創(chuàng)建登錄頁面以接收用戶的名稱和密碼。
index.jsp
<html> <body> <form action="hello"> UserName : <input type="text" name="name"/> <br><br> Password : <input type="text" name="pass"/> <br><br> <input type="submit" name="submit"> </form> </body> </html>
在控制器類中:
HttpServletRequest 用于讀取用戶提供的HTML表單數(shù)據(jù)。 模型包含請求數(shù)據(jù)并將其提供給頁面查看。
HelloController.java
package com.nhooo; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/hello") public String display(HttpServletRequest req,Model m) { //read the provided form data String name=req.getParameter("name"); String pass=req.getParameter("pass"); if(pass.equals("admin")) { String msg="Hello "+ name; //add a message to the model m.addAttribute("message", msg); return "viewpage"; } else { String msg="Sorry "+ name+". You entered an incorrect password"; m.addAttribute("message", msg); return "errorpage"; } } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMVC</display-name> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- Provide support for component scanning --> <context:component-scan base-package="com.nhooo" /> <!--Provide support for conversion, formatting and validation --> <mvc:annotation-driven/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
要運(yùn)行此示例,以下視圖組件必須位于WEB-INF/jsp目錄中。
viewpage.jsp
<html> <body> ${message} </body> </html>
errorpage.jsp
<html> <body> ${message} <br><br> <jsp:include page="/index.jsp"></jsp:include> </body> </html>
輸出: