一、接收前端页面的值
1、传统方式(request)
request.getParameter(“控件名字”);
2、restful风格
@requestParam(“控件名字”)
restful风格是指注解既不用在类上面也不用在方法上面也不用在属性上面
3、普通方式
要跟控件名字一模一样
4、封装成对象
属性名字要跟控件名字一模一样
二、将acton中的值带到页面中
1、传统方式(request范围)
2、ModelMap
3、ModelAndView
4、@ModelAttribute()
用@ModelAttribute()进行修饰的方法会先于这个类中的其他方法先被执行
三、示例action代码
package com.ruide.action;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.ruide.po.User;
@Controller
public class DoLoginAction {
/*@RequestMapping("/login.do")
public String login(HttpServletRequest request){
String username=request.getParameter("username");
String userpass=request.getParameter("userpass");
System.out.println(username+" "+userpass);
return null;
}*/
/*@RequestMapping("/login.do")
public String login(@RequestParam("username") String username,
@RequestParam("userpass") String password
){
System.out.println(username+" "+password);
return null;
}*/
/*@RequestMapping("/login.do")
public String login(String username,String userpass){
System.out.println(username+" "+userpass);
return null;
}*/
/*@RequestMapping("/login.do")
public String login(User user){
System.out.println(user.getUsername()+" "+user.getUserpass());
return null;
}*/
//值传递
/*@RequestMapping("/login.do")
public String login(User use,HttpServletRequest request){
request.setAttribute("username",use.getUserpass());
return "index";
}*/
/*@RequestMapping("/login.do")
public String login(User use,ModelMap mm){
mm.put("username",use.getUsername());
return "index";
}*/
/*@RequestMapping("/login.do")
public ModelAndView login(User use){
Map<String,String> model=new HashMap<String,String>();
model.put("username",use.getUsername());
ModelAndView mv=new ModelAndView("index", model);
return mv;
}*/
//用的最普遍
@RequestMapping("/login.do")
public ModelAndView login(User use){
ModelAndView mv=new ModelAndView();
mv.addObject("username",use.getUsername());
mv.setViewName("index");
return mv;
}
@ModelAttribute("name")
public String getName(){
return "hello world";
}
}