具体例子可以戳这里,挺详细的一个springmvc新手教程,因为教程对配置使用没有太多说明,所以特意写了篇笔记(参考教程例子)。
1.控制器的注解使用
注解控制器例:
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
@Controller是指该类作为这个web端的控制器。
@RequestMapping("/hello")是指把访问路径映射为"/hello",即没有spring时servlet访问映射路径,可以自己设置的映射访问路径。对URL映射不陌生的话,应该知道可以利用URL传参
URL传参例:(一旦设置的方法有了参数,那么你的访问路径就要加参数)
@RequestMapping(value="/hello", method=RequestMethod.GET)
public String getMessage(ModelMap model, @RequestParam String messageR){
model.addAttribute("message", messageR);
return "hello";
}
//这样你的访问路径应该变成“/hello?messagemessage”
另一种传参方式:
@RequestMapping(value="/hello/{messageRecive}", method=RequestMethod.GET)
public String getMessage(ModelMap model, @PathVariable("messageRecive") String messageR){
model.addAttribute("message", messageR);
return "hello";
}
//这样你的访问路径应该变成“/hello/messagemessage”
@RequestMapping(method = RequestMethod.GET)规定了请求的方式,即处理GET请求使用该方法。
ModelMap model是一个交互容器,可以让你管理组件。addAttribute(id,value)让你可以添加数据到容器中,以便数据共享。
调用组件数据例:
<body>
<h2>${message}</h2>
</body>
return "hello"指返回一个名称为hello的页面模板,那么它的全称与servlet的xml配置相关
servlet格式配置例:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
prefix是前缀,suffix是后缀,也就是规定了模板的名称全称。如上面说到的“hello”,通过xml的配置规定,全称应该是“/WEB-INF/jsp/hello.jsp”,这样控制器就能顺利返回一个正确的页面模板。所以如果你没有规定模板格式,那么你就应该在处理方法中返回一个正确的路径及文件“/WEB-INF/jsp/hello.jsp”。
2.页面的控制
参数传递中,上面已经说了利用URL传参的方法了。这里再看一种URL+HTML传参的方法,注意用的是get方法
HTML传参例:
//信息登记页面
<form action="hello" method="get">
信息: <input type="text" name="message"><br />
<input type="submit" >
</form>
//信息接受页面
@RequestMapping(value="/hello", method=RequestMethod.GET)
public String getMessage(ModelMap model, @RequestParam String messageR){
model.addAttribute("message", messageR);
return "hello";
}
如果要用post方法,推荐使用spring的表单,而不使用html自带的表单,便于spring管理数据(我们可以把post的表单变成实例)
spring表单传参例子:
//相应初始页面及创建实例对象(student)
@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("student", "command", new Student());
}
//表单
<h2>Student Information</h2>
<form:form method="POST" action="/HelloWeb/addStudent">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="age">Age</form:label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td><form:label path="id">id</form:label></td>
<td><form:input path="id" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
//对应方法
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("SpringWeb")Student student,
ModelMap model) {
model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId());
return "result";
}
上面是一种将post表单转换成一个实例对象(student)的方法,还有另一种方法
表单传参实例:
//初始页面响应及实例创建
@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student(MoldelMap model) {
Student stu=new Student();
//这里就可以提前设置student的值
model.addAttribute("student",stu);
return "student";
}
//表单,注意model中student实例的名称一致
<form:form method="POST" action="/HelloWeb/addStudent" modelAttribute="student">
......//见上一个
</form:form>