向页面传值的方式

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<String,Object> data=new HashMap<String,Object>();
//相当于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" %>
<html>
<head></head>
<body style="font-size:30px;">
<form action="login.do" method="post">
帐号:<input name="adminCode"/><br/>
密码:<input type="password"  name="pwd"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>

applicationContest.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: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">
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置组件扫描 -->
<context:component-scan  base-package="controller"/>
<!-- 配置mvc注解扫描 -->
<mvc:annotation-driven/>
</beans>

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" version="2.5">
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 
    DispatcherServlet的初始化方法在执行时,
    会启动spring容器,可以通过初始化参数
    指定spring配置文件的位置。
     -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

.JAR包

spring-webmvc 4.1.7

javax.servlet-api 3.1.0


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值