@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping(value="/findAll",method="RequestMethod.GET")
public String findAll(Model model){
System.out.println("表现层:查询所有账户...");
// 调用service的方法
List<Account> list = accountService.findAll();
model.addAttribute("list",list);
return "list";
}
}
<a href="account/findAccount?accountId=10&accountName=zhangsan">查询账户</a>
@RequestMapping("/findAccount")
public String findAccount(Integer accountId,String accountName) {
System.out.println("查询了账户+accountId+","+accountName);
return "success";
}
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
//getters and setters
}
<form action="account/saveAccount" method="post">
账户名称: <input type="text" name="name" ><br/>
账户金额: <input type="text" name="money" ><br/>
<input type="submit" value="保存">
</form>
@RequestMapping("/saveAccount")
public String saveAccount(Account account) {
System.out.println("保存了账户 "+account);
return "success";
}
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
private Address address;
//getters and setters
}
public class Address implements Serializable {
private String provinceName;
private String cityName;
//getters and setters
}
<form action="account/saveAccount" method="post">
账户名称: <input type="text" name="name" ><br/>
账户金额: <input type="text" name="money" ><br/>
账户省份: <input type="text" name="address.provinceName" ><br/>
账户城市: <input type="text" name="address.cityName" ><br/>
<input type="submit" value="保存">
</form>
@RequestMapping("/saveAccount")
public String saveAccount(Account account) {
System.out.println("保存了账户 "+account);
return "success";
}
<a href="account/findAccount?name=zhangsan&name=lisi">查询账户</a>
@RequestMapping("/findAccount")
public String findAccount(String[] name) {
return "success";
}
@RequestMapping("/testRequestParam")
public String testRequestParam(@RequestParam(name="name") String username){
System.out.println(username);
return "success";
}
@RequestMapping(value="/testPathVariable/{sid}")
public String testPathVariable(@PathVariable(name="sid") String id){
System.out.println(id);
return "success";
}
public String testServlet(HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession();
ServletContext servletContext = session.getServletContext();
return "success";
}
public class BaseController{
protected HttpServletRequest request;
protected HttpServletResponse response;
protected HttpSession session;
public void getServletApi(HttpServletRequest request,HttpServletResponse response,HttpSession session){
this.request = request;
this.response = response;
this.session = session;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |