@RequestMapping和@Requestparam注释的使用

1.建立序列化接口User.java

 

package masterSpringMvc.domain;
import java.io.Serializable;
public class User implements Serializable
{
    private String account;
    private String email;
    private String username;
    
    public User()
    {
        super();
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    
    
}
2.建立控制器 UserController

 

package masterSpringMvc.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import masterSpringMvc.domain.User;
@Controller
@RequestMapping(value="/user")
public class UserController
{
    private static List userList;

    public UserController()
    {
        super();
        userList = new ArrayList();
    }

    @RequestMapping(value="/register",method=RequestMethod.GET)
    public String registerForm()
    {
        return "registerForm";
    }

    @RequestMapping(value="/register",method=RequestMethod.POST)
    public String register(
            @RequestParam("account") String account,
            @RequestParam("email") String email,
            @RequestParam("username") String username
            )
    {

        User user=new User();
        user.setAccount(account);
        user.setEmail(email);
        user.setUsername(username);

        userList.add(user);
        return "loginForm";
    }

    @RequestMapping("/login")
    public String login(
            @RequestParam("account") String account,
            @RequestParam("email") String email,
            @RequestParam("username") String username,
            Model model
            )
    {

        for(User user:userList)
        {
            if(user.getAccount().equals(account))
            {
                model.addAttribute("user",user);
            }
        }
        return "loginForm";
    }
}
3.建立模版文件
registerForm.html
loginForm.html
4.使用
注册用户
使用注册用户进行登录
登录成功