Spring MVC Learning Note-Passing a value to a controller

Source: Internet
Author: User

  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.