MVC设计模式
M(Model):模型
Dao,Service,实体类
V(View):视图
页面
C(Controller):控制器
servlet
JSP Model2开发模式
优点
松耦合+高重用性+高扩展性
缺点
程序结构复杂,效率低
SpringMVC配置流程
导入jar包
在web.xml中配置SpringMVC的dispatcherServlet
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- init-param初始化参数用于配置读取指定的配置文件 -->
<!-- 若不配置此项则默认会读取/WEB-INF/<servlet-name>-servlet.xml -->
<init-param>
<!-- contextConfigLocation是固定名称 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 此处用“/”而非“/*”是为了只映射非jsp的请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
配置mvc映射
在上面提到的applicationContext-mvc.xml中配置mvc映射,此配置文件放在resources文件夹下
<!-- 映射请求的url和控制器(HandlerMapping) -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
</bean>
<!-- 处理视图和jsp的映射(视图解析器ViewResolver) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置通用的前后缀 -->
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
绘制视图
写一个页面作为controller的结果页面,其中可用el表达式从request中取值
编写controller控制器
第一种:继承AbstractController
controller部分
controller控制器继承AbstractController
public class HelloController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String hello = request.getParameter("hello");
request.setAttribute("xxx", "hello");
// 返回的是一个模型视图对象
// 配置文件中设置了通用前后缀,所以此处不需要再写成“/Welcome.jsp”
ModelAndView mav = new ModelAndView("Welcome");
// addObject方法将键值对放入request
mav.addObject("xxx", hello);
return mav;
}
}
Spring MVC的配置文件配置部分
<!-- 定义控制器的bean,此bean的别名(name)要与请求的url地址相同 -->
<bean name="/hello.html" class="controller.HelloController">
</bean>
第二种:使用注解编写控制器
controller部分
注意:不再继承AbstractController
在类上添加Controller注解,在其中的方法上通过RequestMapping注解配置其对应的url(请求要发送到此url来触发对应方法)
@Controller
public class HelloController {
// 简写的RequestMapping
@RequestMapping("/hello.html")
protected ModelAndView hello(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String hello = request.getParameter("hello");
request.setAttribute("xxx", "hello");
// 配置文件中设置了通用前后缀,所以此处不需要再写成“/Welcome.jsp”
ModelAndView mav = new ModelAndView("Welcome");
mav.addObject("xxx", hello);
return mav;
}
// 完整的RequestMapping,value为请求地址,method为请求方式,params为方法入参
@RequestMapping(value="/login.html", method=RequestMethod.GET, params="username")
// 当RequestMapping的params未明确指出映射关系时,要在入参列表中用@RequestParam标注映射参数
protected ModelAndView login(@RequestParam String username) throws Exception {
String hello = request.getParameter("hello");
request.setAttribute("xxx", "hello");
// 配置文件中设置了通用前后缀,所以此处不需要再写成“/Welcome.jsp”
ModelAndView mav = new ModelAndView("Welcome");
mav.addObject("xxx", hello);
return mav;
}
}
Spring MVC的配置文件配置部分
不再使用BeanNameUrlHandlerMapping的bean,有两种方法来支持注解
第一种方法:配置两个bean
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
</bean>
第二种方法:配置一个mvc:annotation-driven,同时添加context注解扫描
<!-- beans根节点属性中添加 -->
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd" >
<!-- 子节点添加 -->
<mvc:annotation-driven />
<context:component-scan base-package="controller" />
参数传递
View传入Model
使用同名入参传递
提交表单中
<input type="text" name="username" value="root"/>
controller中
public void login(String username){
ModelAndView mav = new ModelAndView("Welcome");
mav.addObject("xxx", username);
return mav;
}
传递参数的name和controller方法入参的名称相同时,可以直接绑定;此时传参名称不相同会抛异常
controller入参中也可以有对象,当表单中的数据name与controller入参对象的属性名相同时,可以自动赋值给对象属性
不同名入参加注解传递
提交表单中
<input type="text" name="username" value="root"/>
controller中
public void login(@RequestParam(value="username" required="true")String myname){
ModelAndView mav = new ModelAndView("Welcome");
mav.addObject("xxx", username);
return mav;
}
传递参数的name和controller方法入参的名称不相同时,用RequestParam注解标注使用的传过来的值的name,required属性表示是否必填,默认为true
传递对象
若传入的是一个对象,如:User(String username, String password)
可以直接使用与其属性相同的入参来传递
public void login(String username, String password){
...
}
model传入view
ModelAndView方法
- addObject
- addAllObject
- setView
- setViewName
Model方法
SpringMVC在调用方法之前会创建一个隐含的Model对象用于传递信息,将其放在入参位置直接通过其 addAttribute(键值对) 的方法来添加要传递到view的信息
return时直接返回代表结果view的字符串
public void login(String username, Model model){
model.addAttribute("xxx", username);
return "Welcome";
}