(五)ModelAndView的使用(用于返回模型M和视图V数据----渲染页面)

1.ModelAndView的作用:

MVC模型中的,M为模型(Model),V为视图(View)。ModelAndView 是一种变量类型,这类类型包含了  模型和视图  的数据。但返回ModelAndView 时,即把模型数据和视图数据一起返回。一般把ModelAndView 数据返回给浏览器,若浏览器根据模型数据和视图数据来渲染网页。等于ModelAndView把Model 和 View 的数据打包在一起。


2.ModelAndView的使用:

多写在C(控制器)中。一个方法若要返回 ModelAndView 变量,必需在这个方法的参数里有 ModelAndView,如:

public ModelAndView func(int a,String b,ModelAndView mv){
             ........
             return mv;
}


3.例子:

例子完成的功能:用户输入信息,然后信息在网页上显示出来。(用户返回的信息由ModelAndView返回)

(一)基本环境配置:

(目录树:)



代码运行过程:



(二):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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>pro4</display-name>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>


(三)spring-mvc.xml  :( springmvc的核心配置文件)

<?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:mvc="http://www.springframework.org/schema/mvc"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="
	   	http://www.springframework.org/schema/beans
	   	http://www.springframework.org/schema/beans/spring-beans.xsd
	   	http://www.springframework.org/schema/mvc
	   	http://www.springframework.org/schema/mvc/spring-mvc.xsd
	   	http://www.springframework.org/schema/context
	   	http://www.springframework.org/schema/context/spring-context.xsd">
	   	<!-- 上面是固定约束,下面是进行springmvc配置 -->
	   	<!-- 下三行分别是,开启注解扫描,配置映射器,配置适配器,配置视图解析器 -->
	   	<context:component-scan base-package="controller"></context:component-scan>
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix">
				<value>/WEB-INF/</value>	<!-- 表示要调用的文件前缀,即表示要调用的文件在/WEB-INF/路径下 -->
			</property>
			<property name="suffix">
				<value>.jsp</value>			<!-- 表示要调用的文件后缀,即表示是jsp文件 -->
			</property>
		</bean>
	</beans>


(四)需要引入的jar包

导入到WebRoot下的WEB-INF下的lib 路径下:



=======================================================================================

java代码:

(一)持久类 User.java 用于记录用户需要输入的信息(属于MVC中的M)

package po;

import java.io.Serializable;

public class User  implements Serializable{
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}



(二)控制器:UserController.java:(属于MVC中的C)

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import po.User;

@Controller
public class UserController {
	/*当输入http://localhost:8080/项目名/loginForm   时,跳转至/WEB-INF/loginForm.jsp*/
	@RequestMapping(value="/loginForm",method=RequestMethod.GET)
	public String loginForm(){
		return "loginForm";	//loginForm.jsp
	}
	
	@RequestMapping(value="/show_message",method=RequestMethod.POST)
	public ModelAndView showMessage(@RequestParam("username") String username,
							@RequestParam("password") String password,ModelAndView mv){
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		
		mv.addObject("USER", user);	//为ModelAndView变量设置Model(Model即数据)
		mv.setViewName("show");		//为ModelAndView变量设置View(该视图View 为/WEB-INF/show.jsp)
		return mv;					//向客户端(浏览器)返回ModelAndView 数据
	}
}

====================================================================

JSP代码:(中MVC中的V)表示视图:

(一)loginForm.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>LoginForm</title>
</head>
<body>
	<form action="show_message" method="post">
		<table>
			<tr>
				<td>用户名:</td>
				<td><input type="text" id="username" name="username"></td>
			</tr>
			<tr>
				<td>密码:</td>
				<td><input type="password" name="password"></td>
			</tr>
			<tr><td><input type="submit"></td></tr>
		</table>
	</form>
</body>
</html>


(二)show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>show message</title>
</head>
<body>
	${requestScope.USER.username}
	${requestScope.USER.password}
	<%
		//上面是用KL表达式获取数据,下面使用内嵌java语法来获取数据,两种方法选一种即可
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		out.println(username);
		out.println(password);
	 %>
</body>
</html>


===================================================================================

测试结果:

输入 :http://localhost:8080/model/loginForm  (其中model为我的项目名)

出现登录窗口

,输入信息后按提交。

就会调用show.jsp 打印信息。红框处的 show_message 为 show.jsp的映射路径而已。


其中一次信息是KL表达式打印的,一次是java内嵌代码打印的。

















  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值