获取请求参数值的方式

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("/login.do")
public String checkLogin(HttpServletRequest request){
System.out.println("checkLogin()");
String adminCode = request.getParameter("adminCode");
String pwd=request.getParameter("pwd");
//System.out.println(adminCode+ " " + pwd);
return "index";
}

//读取请求参数值的第二种方式:
//使用@RequestParam
//最好每个形参前都添加@RequestParam。
@RequestMapping("/login2.do")
public String checkLogin2(String adminCode,@RequestParam("pwd") String password){
System.out.println("checkLogin2()");
System.out.println(adminCode + " " + password);
return "index";
}


//读取请求参数值的第三种方式
//封装成javabean
@RequestMapping("/login3.do")
public String checkLogin3(Admin admin){
System.out.println("checkLogin3()");
System.out.println(admin.getAdminCode()+ " " + admin.getPwd());
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

你可能感兴趣的:(springMVC)