向页面传值的方式

package controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
/**
 * 处理器类:
 */
@Controller
public class HelloController {
@RequestMapping("/toLogin.do")
public String toLogin(){
System.out.println("toLogin()");
return "login";
}
//向页面传值的第一种方式:
//绑订数据到request对象。

@RequestMapping("/login4.do")
public String checkLogin4(Admin admin,HttpServletRequest request){
System.out.println("checkLogin4()");
System.out.println(admin.getAdminCode());
//将数据绑订到request
request.setAttribute("adminCode",admin.getAdminCode());
//springmvc默认使用转发机制来跳转。
return "index";
}

//向页面传值的第二种方式:
//返回ModelAndView对象。

@RequestMapping("/login5.do")
public ModelAndView checkLogin5(Admin admin){
System.out.println("checkLogin5()");
System.out.println(admin.getAdminCode());
Map data=new HashMap();
//相当于request.setAttribute
data.put("adminCode", admin.getAdminCode());
ModelAndView mav = new ModelAndView("index",data);
return mav;
}

//向页面传值的第三种方式:
//使用ModelMap作为方法的入参。

@RequestMapping("/login6.do")
public String checkLogin6(Admin admin,ModelMap mm){
System.out.println("checkLogin6()");
System.out.println(admin.getAdminCode());
//相当于request.setAttribute
mm.addAttribute("adminCode", admin.getAdminCode());
return "index";
}

//向页面传值的第四种方式:
//将数据绑订到session对象。

@RequestMapping("/login7.do")
public String checkLogin7(Admin admin,HttpSession session){
System.out.println("checkLogin7()");
System.out.println(admin.getAdminCode());
session.setAttribute("adminCode",admin.getAdminCode());
return "index";
}

}

其中,jsp,配置文件,web.xml如下:

login.jsp

<%@page pageEncoding="utf-8"  contentType="text/html; charset=utf-8" %>




帐号:

密码:





applicationContest.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">










web.xml



 
    springmvc
    org.springframework.web.servlet.DispatcherServlet
   
   
        contextConfigLocation
        classpath:applicationContext.xml
   

    1


    springmvc
    *.do


.JAR包

spring-webmvc 4.1.7

javax.servlet-api 3.1.0


你可能感兴趣的:(向页面传值的方式)