spring mvc controlle

@PathVariable annotated parameters for access to URI template variables. See the section called “URI Template Patterns”.
@MatrixVariable annotated parameters for access to name-value pairs located in URI path segments. See the section called “Matrix Variables”.
@RequestParam annotated parameters for access to specific Servlet request parameters. Parameter values are converted to the declared method argument type. See the section called “Binding request parameters to method parameters with @RequestParam”.
@RequestHeader annotated parameters for access to specific Servlet request HTTP headers. Parameter values are converted to the declared method argument type.
@RequestBody annotated parameters for access to the HTTP request body. Parameter values are converted to the declared method argument type using HttpMessageConverters. See the section called “Mapping the request body with the @RequestBody annotation”.
@RequestPart annotated parameters for access to the content of a "multipart/form-data" request part. See Section 16.11.5, “Handling a file upload request from programmatic clients” and Section 16.11, “Spring’s multipart (file upload) support”.

@RequestMapping   使用@请求映射注释将URL(例如/约会)映射到整个类或特定的处理程序方法。通常,类级注释将特定的请求路径(或路径模式)映射到窗体控制器,附加的方法级注释缩小特定HTTP方法请求方法(“GET”、“POST”等)的主映射或HTTP请求参数条件。

Invalid ordering of BindingResult and @ModelAttribute. 

@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, Model model, BindingResult result) { ... }

Note, that there is a Model parameter in between Pet and BindingResult. To get this working you have to reorder the parameters as follows:

@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, Model model) { ... }

@PathVariable 注释获取URI参数的变量

/owners/42/pets/21

@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {

    @RequestMapping("/pets/{petId}")
    public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
        // implementation omitted
    }

}

@ RequestParam注释将请求参数绑定到控制器中的方法参数。(默认情况下使用此注释的参数是必需的,但是可以通过将@ RequestParam的必需属性设置为false(例如,@ RequestParam(value = id),需要= false)来指定参数是可选的。)

@Controller
@RequestMapping("/pets")
@SessionAttributes("pet")
public class EditPetForm {

    // ...

    @RequestMapping(method = RequestMethod.GET)
    public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
        Pet pet = this.clinic.loadPet(petId);
        model.addAttribute("pet", pet);
        return "petForm";
    }

    // ...

}

@RequestBody 指出请求体方法参数注释,方法参数应该绑定到HTTP请求体的值。

@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
    writer.write(body);
}

实例:学生系统增删改

StudentController.java

package com.huawei.controller;



import java.util.ArrayList;
import java.util.List;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import com.huawei.model.Student;


@Controller
@RequestMapping("/student")
public class StudentController {


private static List<Student> studentList=new ArrayList<Student>();

static{
studentList.add(new Student(1,"张三",11));
studentList.add(new Student(2,"李四",12));
studentList.add(new Student(3,"王五",13));
}

@RequestMapping("/list")
public ModelAndView list(){
ModelAndView mav=new ModelAndView();
mav.addObject("studentList", studentList);
mav.setViewName("student/list");
return mav;
}

@RequestMapping("/preSave")
public ModelAndView preSave(@RequestParam(value="id",required=false) String id){
ModelAndView mav=new ModelAndView();
if(id!=null){
mav.addObject("student", studentList.get(Integer.parseInt(id)-1));
mav.setViewName("student/update");
}else{
mav.setViewName("student/add");
}
return mav;
}

@RequestMapping("/save")
public String save(Student student){
if(student.getId()!=0){
Student s=studentList.get(student.getId()-1);
s.setName(student.getName());
s.setAge(student.getAge());
}else{
studentList.add(student);
}
// return "redirect:/student/list.do";
return "forward:/student/list.do";
}

@RequestMapping("/delete")
public String delete(@RequestParam("id") int id){
studentList.remove(id-1);
return "redirect:/student/list.do";
}

}

add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/student/save.do" method="post">
<table>
<tr>
<th colspan="2">学生添加</th>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
</body>

</html>

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/student/preSave.do">添加学生</a>
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<c:forEach var="student" items="${studentList }">
<tr>
<td>${student.id }</td>
<td>${student.name }</td>
<td>${student.age }</td>
<td>
<a href="${pageContext.request.contextPath}/student/preSave.do?id=${student.id}">修改</a>
&nbsp;&nbsp;
<a href="${pageContext.request.contextPath}/student/delete.do?id=${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>


</body>
</html>

update.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/student/save.do" method="post">
<table>
<tr>
<th colspan="2">学生修改</th>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name" value="${student.name }"/></td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" value="${student.age }"/></td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="id" value="${student.id }"/>
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
</body>

</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<% response.sendRedirect("student/list.do"); %>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值