In Spring MVC, the front-end data is passed back

Source: Internet
Author: User

In Spring MVC, the front-end data is passed back In Spring MVC, the front-end data and back-end data are the two directions of data flow, first of all, the front-end data transmission is described.

In general, the front-end data transmission of the scene, most of the presentation of the form, the content of form form in the back-end learning of the MD document, the form tag syntax format is as follows

The main is the introduction of three parameters, that is, name, method and action, where name can be omitted, just a marker symbol, the use is not too large, and the action represents the method of processing the form, Method means that the way to transfer data to the backend is by default the Get method, which is a basic introduction.

Front Page


data is obtained from the front-end submission form, so you must first have a form where you use the Freemarker view page, named LOGIN.FTL , and the name of the form "login". Where the action in the form represents the background URL (method) to process this "login" form, which is the URL (method) within the corresponding controller, which is exactly in, where L The OGIN.FTL is as follows:

@Controller@RequestMapping("/freemarker")public class LoginController {    private Logger logger = LoggerFactory.getLogger(LoginController.class);    @RequestMapping(value = "/gotologin", method = RequestMethod.GET)    public String gotoLogin() {        //跳转到登录的login页面        logger.debug("正在跳转到login页面!");        return "login";    }    @RequestMapping(value = "/login", method = {RequestMethod.GET, RequestMethod.POST})    public String login(String name, String gender, int age, String password, Model model) {        //从页面之中提取输入的信息,并且封装好        model.addAttribute("name", name);        model.addAttribute("gender", gender);        model.addAttribute("age", age);        model.addAttribute("password", password);        //获取了页面的信息之后,就将信息发送到想要展示的页面        logger.debug("name: " + name + ", gender: " + gender + ", age: " + age + ", password: " + password);        return "showinfo";    }}
Front desk Display

In fact, completed the above two steps, we have transferred from the foreground data, completed the background processing, and the data stored in the model, but in this case, the data is not displayed, the results are not intuitive, then, at this time need to be processed back-end data, displayed in the front page, To do this we create a page that displays the information after the login, named SHOWINFO.FTL


Complete the above three steps, completed from the front desk input, to the background processing, and then to the front-end display of the process, as follows:

st=>start: 开始e=>end: 结束op1=>operation: 前台输入op2=>operation: 后台处理op3=>operation: 前台展示st(right)->op1(right)->op2(right)->op3(right)->e
Background direct data to the front end

In fact, if there is no front desk input, the background to build their own data, can also be directly to the front desk, as follows:

st=>start: 开始e=>end: 结束op2=>operation: 后台处理op3=>operation: 前台展示st(right)->op2(right)->op3(right)->e

The code operates as follows:

  @Controllerpublic class Tofrontcontroller {private static Logger Logger = Loggerfactory.getlog    GER (Tofrontcontroller.class); When the value is passed to the front-end page, it is generally necessary to assist through the model, there is no need to model or close to the method of auxiliary processing//here actually do not need to pass the value, because do not receive the value of the front end, just need to set their own inside the @RequestMapping (        Value = "/tofronttest") public String Tofront (model model) { ("Tofront method called, should return Tofrontinfo view!");        User User1 = new user ();        User1.setage (24);        User1.setname ("Wangsan Lee");        User1.setpassword ("dfasfagasdfsdafgyrt75");        System.out.println (User1.getname () + "," + user1.getage () + "," + User1.getpassword ());        Model.addattribute ("user1", User1);    return "Tryandlearn/tofrontinfo"; }} 
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%><%@ taglib prefix= "form" uri= "/HTTP/ "%><%@ taglib prefix=" SF1 "uri=" "% ><%@ taglib prefix= "from" uri= "Http://"%>
The sense of dislocation in operation
<%@ taglib prefix= "C" uri= "Http://"%><%@ page contenttype= "text/html;charset= UTF-8 "language=" java "%><%@ taglib prefix=" SF "uri=" Http:// "%>

We know that the front end is a display page, the main display of data and pages, of course, but also to collect data, but the total is to collect data, for example, we want to log in, the login page is login.html, but if you want to reach this page, we need a link to jump to this page , in <a href= "/freemarker/gotologin" > Jump to the Login home page </a></br>, the URL to handle this jump, yes, we jump to the Http://localhost:8080/freemarker/gotologin , but this is determined by the controller in the background, among the Gotologin methods in the first paragraph of Logincontroller , just decided to jump to the login page, so there is a latency , we want to achieve, we want to do as the results of our current action appear , that is, for example, we want to reach the login page, So this result is bound to be the result of our current action, and what is the current action? That is to jump to the login page, if we log in, want to show the login information, the two operations are coherent, login, and then show, that is, login-->show, means that show is the result of the login, then show will appear as a result, The login method of the first code is shown, that is, the login method returns Showinfo, and the information needed in Showinfo needs to be retrieved in the back-end information.

<!-- freemarker获取信息 -->

In Spring MVC, the front-end data is passed back

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: 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.