springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 视图解析器、资源视图拼接 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 自动扫描包下的注解 -->
<context:component-scan base-package="com.lwj.controller"></context:component-scan>
</beans>
控制类
package com.lwj.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
//@Controller只是声明了当前类为处理器类,必须搭配@RequestMapping才能生效
@Controller
// 命名空间(父路径),表示当前类的所有请求路径都在/myController之下(可以不写)
@RequestMapping(value = "/myController")
public class MyController {
/*
* 处理器中,常用的参数有以下五类
* HttpServletRequest
* HttpServletResponse
* HttpSession
* 用于承载数据的Model
* 请求中所携带的请求参数:表单的参数
*
*
*/
/*
* 一个类下可以定义多个请求
*
* 可以使用 method 指定请求提交方式,默认两种都可以 只有表单和ajax能提交post请求
*
* 请求路径可以使用通配符:"/*first.action","/first*.action","/* /first.action.action"等等
*/
// 两个路径都能定位到当前请求 method = RequestMethod.POST 表示只接收 post 请求
@RequestMapping(value = { "/first.action", "/first2.action" }, method = RequestMethod.POST)
public ModelAndView firstRequest(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView();
// 其实底层执行的是request.setAttribute()方法
mv.addObject("msg", "Hello");
// 添加视图名称:路径+文件名
mv.setViewName("/WEB-INF/jsp/welcome.jsp");
return mv;
}
}
接收表单数据
//接收表单传送过来的数据,名字必须对应,框架能帮你做类型转换
@RequestMapping(value = "/second.action")
public ModelAndView secondRequest(String name,String password) {
ModelAndView mv = new ModelAndView();
// 其实底层执行的是request.setAttribute()方法
mv.addObject("name", name);
mv.addObject("password", password);
// 添加视图名称:路径+文件名
mv.setViewName("/WEB-INF/jsp/welcome.jsp");
return mv;
}
//当和表单名字不一致时,fname、fpassword 为表单名称
@RequestMapping(value = "/second.action")
public ModelAndView secondRequest(@RequestParam("fname") String name,@RequestParam("fpassword") String password) {
ModelAndView mv = new ModelAndView();
// 其实底层执行的是request.setAttribute()方法
mv.addObject("name", name);
mv.addObject("password", password);
System.out.println("name="+name+":password="+password);
// 添加视图名称:路径+文件名
mv.setViewName("/WEB-INF/jsp/main.jsp");
return mv;
}
//接收实体对象,名称必须对应
@RequestMapping(value = "/second.action")
public ModelAndView secondRequest(User user) {
ModelAndView mv = new ModelAndView();
System.out.println(user.toString());
mv.setViewName("/WEB-INF/jsp/main.jsp");
return mv;
}
//若实体中存在其他实体变量,例如 Student 中有个 School 变量,则表单名称写法 School.变量名
处理器返回方法 // 两个路径都能定位到当前请求
//当不需要携带数据,仅仅是进行页面跳转时,可以返回一个字符串
@RequestMapping(value = { "/first.action","/first2.action"})
public String firstRequest() {
//结合视图解析器使用
return "welcome";
}
//需要携带数据,也需要进行页面跳转时,返回一个 ModelAndView
@RequestMapping(value = "/second.action")
public ModelAndView secondRequest(User user) {
ModelAndView mv = new ModelAndView();
mv.addObject(user);
mv.setViewName("/WEB-INF/jsp/main.jsp");
return mv;
}
还有 void 和 object 返回类型-------省略
void 类型通过 request 等跳转或传递数据