Java study Note 10: Get POST data and simple template output

Source: Internet
Author: User

Get POST Data
This section will continue with Java Study Notes 09.

Generally, during development, we need to obtain the data submitted in the form, so we must first create a say. jsp. The content of this jsp is a very simple form. It is submitted in POST mode and submitted to the/hello/show/path.
[Java]
<Form action = '/hello/show/' method = "post">
<Input name = "username" type = "text"/>
<Input name = "password" type = "text"/>
<Input name = "" type = "submit"/>
</Form>
Then we need a controller file, two actions, one is the real say. jsp static page, and the other is to receive and process the data submitted by POST.
The sya () method displays static pages, and the show () method processes POST data.
The show method has two parameters. The User user object Spring will automatically fill the POST data to the User class. Modelmodel is mainly used to transmit data between the Controller and the template.
[Java]
Package com. mvc. rest;
 
Import org. springframework. stereotype. Controller;
Import org. springframework. ui. Model;
Import org. springframework. web. bind. annotation. RequestMapping;
 
// @ Controller is an annotation label that identifies the Controller class. This annotation is required for Controller classes.
@ Controller
// @ RequestMapping (value = "/hello") will be mapped to url/hello/, then access the Action in HelloController
@ RequestMapping (value = "/hello ")
Public class HelloController {

// @ RequestMapping (value = "/say") is mapped to url/hello/say, and then the Action in HelloController is accessed.
@ RequestMapping (value = "/say ")
Public void say (){
System. out. print ("this is HelloController And say Action \ r \ n ");
}

@ RequestMapping ("/show ")
Public String show (User user, Model model ){
System. out. print (user. getUsername ());
System. out. print (user. getPassword ());
Model. addAttribute ("user", user );
Return "hello/show ";
}
}
User class:
[Java]
Package com. mvc. rest;
 
Public class User {
Private String username;

Private String password;
 
Public String getUsername (){
Return username;
}
 
Public void setUsername (String username ){
This. username = username;
}
 
Public String getPassword (){
Return password;
}
 
Public void setPassword (String password ){
This. password = password;
}

 
}
The show. jsp template class outputs the received POST data:

[Java]
User: $ {user. username} <br/>
Password: $ {user. password}

Bean. xml configuration:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans
Xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<Context: annotation-config/>
<Aop: aspectj-autoproxy/> www.2cto.com
<Bean id = "User" class = "com. mvc. rest. User"> </bean>
</Beans>

 

Related Article

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.