Springmvc Simple Transfer value (implementation code) _java

Source: Internet
Author: User
Tags stub

Before learning Springmvc feeling his value is very magical: simple, fast, efficient.

Write a few simple values today to share with you, hope to be helpful to everyone.

One

To pass forward from the back:

(1)

@Controller

@RequestMapping (value={"/hello"}) public

class Hellocontroller {

 

  @RequestMapping (value={" Sub "}" Public

  Modelandview Submit (HttpServletRequest request) throws Exception {/

    /TODO auto-generated method Stub

    Modelandview m=new Modelandview ();

    M.addobject ("OK", "hello");

    M.setviewname ("Success");
return m;

  }

}

Put what you want to pass in the AddObject (String,object), the value is type Object, everything can be put.

Setviewname () is the setting to which page (success.jsp page) to jump to.

Use ${requestscope} or ${ok in the success.jsp page to remove it. is not very simple and quick.

It can also be passed in this way:

@Controller

@RequestMapping (value={"/user"}) public

class Usercontroller {

  @RequestMapping (value={)    Get '}) public

  Modelandview User (user user) throws Exception {

    Modelandview mv=new modelandview ();    
Mv.addobject ("OK", user.getusername () + "--" +user.getpassword ());
    Mv.setviewname ("Success");
    return MV;
}

}

The front-end is a simple form form:

<form action= "User/get" method= "POST" >

    <input type= "text" name= "username" id= "username" >

    < Input type= "text" name= "password" id= "password" >

    <input type= "Submit" >

</form>

(2) The return value may not be Modelandview

@RequestMapping (value={"/map"}) public

  String OK (map Map,model model,modelmap modelmap,user User) throws Exception {

map.put ("Ok1", user);

    Model.addattribute ("Ok2", user);

    Modelmap.addattribute ("Ok3", user);

Return ' show ';

}

Two

Back in the past:

(1)

@RequestMapping (value={"Ant/{username}/topic/{topic}"},method={requestmethod.get}) public

  Modelandview Ant (

      @PathVariable (value= "username") string username,

      @PathVariable (value= "topic") string topic

      ) throws Exception {

    //TODO auto-generated method stub

    Modelandview m=new Modelandview ();

    SYSTEM.OUT.PRINTLN (username);

    SYSTEM.OUT.PRINTLN (topic);

return m;

  }

The front end is this way:

<a href= "Hello/ant/tom/topic/cat" >ant</a>

Corresponds to the value={"Ant/{username}/topic/{topic}"} One by one.

It can also be in this form:

@RequestMapping (value={"/regex/{number:\\d+}-{tel:\\d+}"}) public

  Modelandview Regex (

      @PathVariable ( value= "number" int number,

      @PathVariable (value= "Tel") String Tel

      ) throws Exception {

    //TODO auto-generated method Stub

    Modelandview m=new Modelandview ();

    SYSTEM.OUT.PRINTLN (number);

    SYSTEM.OUT.PRINTLN (tel);

    return m;

  }

The front end is this way:

<a href= "hello/regex/100-111" >regex (regular) </a>

(2) This is a key transfer value:

@RequestMapping (value={"/ok1"}) public

  string Ok1 (@RequestParam (value= "username") string username) throws Exception {

    System.out.println (username);

    Return ' show ';

  }

The front end is this way:

<a href= "User/ok1?username=tom" > has key transfer value </a>

This is a no key transfer value:

@RequestMapping (value={"/ok2"}) public

  String ok2 (@RequestParam string password, @RequestParam string username) Throws Exception {

   

    System.out.println (username);

    SYSTEM.OUT.PRINTLN (password);

   

    Return ' show ';

  }

The front end is this way:

<a href= "user/ok2?username=tom&password=111" > No key transfer </a>

The interesting thing is that it can accurately correspond to two values.

The above Springmvc simple transfer value (implementation code) is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.