springmvc passing two parameters: the model parameter passing (very commonly)

On a parameter used to write a reference transmission manner, but when the large end of the current data content of the form page, using shaped participants inconvenient, so you can use the model parameter passing, encapsulates the request parameter entities . Examples are as follows:

Still more of a project-based learning.

Packaging entity, you first need to create entity classes:

package com.zs.entity;

import org.springframework.stereotype.Component;

@Component
public class User {
    private int uid;
    private String username;
    private String password;

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

 Add annotations to use spring plant management object while scanning the entity class package entity in the spring package in the main configuration file:

<?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:contect="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <contect:component-scan base-package="com.zs.entity"/>
</beans>

The controller then modify content on a parameter we use mass participation, mass participation model used here

package com.zs.controller;

import com.zs.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/user")
public class UserController {
    /**
     * By the mapping method finds the address, method request mode
     * @Param User to User request parameters encapsulated object, the object is automatically assigned to an entity based on a key parameter in the request,
     * @return
     */
    @RequestMapping(value = "/login",method = {RequestMethod.GET,RequestMethod.POST})
    public String login(User user) {
        /**
         * View the passed parameter, the value can be obtained through the get method
         */
        System.out.println(user);
        System.out.println ( "User Name:" + user.getUsername ());
        System.out.println("密码:"+user.getPassword());

        return "index";
    }
}

Run the program, the form, the results are as follows:

As can be seen, the passed parameter reception key pair in accordance with the attribute name, since there is no transmission UID, so the default value is 0.

3.HttpservletRequest object by reference

From the front desk later stage mass participation also can use our HttpServletRequest object previously used in the stage of mass participation servlet

Examples are as follows:

operation result:

 

Guess you like

Origin www.cnblogs.com/Zs-book1/p/11096809.html