1. Modify Hellocontroller.java
requestmapping indicates which URL is used to correspond to @requestmapping ({"/hello", "/"}) public string hello (@RequestParam ("username") string username) {System.out.println ("Hello"); SYSTEM.OUT.PRINTLN (username); return "Hello";}
Then enter the request in the browser
Http://localhost:8080/springmvc_hello/hello?username=abc
The console can see the value passed
But using the Requestparam, if the request does not pass value, will be reported 400 error, because the default parameter as part of the address
2. The second kind, remove the Requestparam
requestmapping indicates which URL is used to correspond to @requestmapping ({"/hello", "/"}) public String Hello (string username) { System.out.println ("Hello"); SYSTEM.OUT.PRINTLN (username); return "Hello";}
This can be non-pass value, NULL when the value is not passed
Pass the value to the view
1. Use map to transmit values
Hellocontroller.java file
Package Org.common.controller;import Java.util.map;import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.RequestMapping; @Controllerpublic class Hellocontroller {// requestmapping indicates which URL is used to correspond to @requestmapping ({"/hello", "/"}) public String hello (string username,map<string,object > Context) {System.out.println ("Hello"); Context.put ("username", username); SYSTEM.OUT.PRINTLN (username); return "Hello";} @RequestMapping ("/welcome") public String Welcome () {System.out.println ("Welcome"); return "Welcome";}}
hello.jsp file
<%@ 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" >
2. Use model to transmit values
Hellocontroller.java file
Package Org.common.controller;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import org.springframework.web.bind.annotation.RequestMapping; @Controllerpublic Class Hellocontroller {//requestmapping indicates which URL is used to correspond to @requestmapping ({"/hello", "/"}) public String hello (string Username,model Model) {System.out.println ("Hello"), Model.addattribute ("username", username);// equals Model.addattribute ("String", username); Model.addattribute (username);//model.addattribute (new User ());--> Model.addattribute ("User", new user ()); SYSTEM.OUT.PRINTLN (username); return "Hello";} @RequestMapping ("/welcome") public String Welcome () {System.out.println ("Welcome"); return "Welcome";}}
hello.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" >
Spring MVC Learning Note-Passing a value to a controller