jsp传递值到后台controller

虽然开发了两年  一直没有真真正正的自己搭建项目。

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>Insert title here</title>
</head>
<body>
	<form action="helloworld2">
		用户名:<input name="userName" /> 密码:<input name="pass" /> <input
			type="submit" value="登陆">
	</form>
</body>
</html>

java代码:

package com.ly.springmvc.handers;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloWorld {

    /**
     * 1. 使用RequestMapping注解来映射请求的URL
     * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于InternalResourceViewResolver视图解析器,会做如下解析
     * 通过prefix+returnVal+suffix 这样的方式得到实际的物理视图,然后会转发操作
     * "/WEB-INF/views/success.jsp"
     * @return
     */
    @RequestMapping("/helloworld2")
    public String hello(HttpServletRequest request,String userName,@RequestParam("pass") String passWord){
    	userName=request.getParameter("userName");
    	passWord=request.getParameter("pass");
    	
    	if(userName.equals("linyi") && passWord.equals("123456")){
    		
    		return "success";
    	}else{
    		System.out.println("hello world");
    		return "error";
    	}
        
    }
}

总结:

1 form表单使用submit提交,通过action地址映射到 controller中的requestMapping value值。

2 获取页面属性值两种方式:1》 @RequestParam("pass")指定属性值。2》String useName变量与页面属性值一样。

3 HttpServletRequest获取页面属性值:request.getParameter("userName");

4 form表单请求方式 默认为get,传输的参数会显示在地址栏。post不会。

5 return "success"; 跳转页面是从:springmvc-test/WEB-INF/views/success.jsp

猜你喜欢

转载自blog.csdn.net/weixin_37020977/article/details/82468786
今日推荐