一路springmvc 咖
springmvc 能干什么?:
1,controller 为中心完成对系统流程的控制管理,
2,从请求中搜集数据
3,对传人的参数进行验证校验
4,将结果返回给视图---如何展示在页面上
5,对不同的 视图提供不同的解决方案: fremark ,jsp
6,对jsp提供视图标签库
7,拦截器
8,上传文件
springmvc核心组件:
1,dispatchservlet 中央控制器
2,controller 具体处理请求
3,HandlerMapping 映射处理器:负责映射处理器转发给controller 时的映射策略
4,ModelAndView 服务层返回的数据和视图层的封装类 ModelAndView + 5,controller 不就是MVC吗?
6,ViewResolver & view 对不同的视图不同的解析,视图解析器
7,Interceptors : 拦截器,负责定义好的请求
springmvc 支持:配置文件和注解的方式进行开发
注解有几种方式?
@controller 控制层
@Service 业务逻辑层
@Repository 持久层
@Component 组件
springMVC 工程过程:
1,创建工程
2,导入jar
3,dispathservlet 配置 在 web.xml
整个demo如下:
Web.xml:
<?xml version="1.0"encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>lsn_springmvcold414</display-name>
<!-- 中央控制器,配制spring分发器servlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-Servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Springmvc-Servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 需要配置controller,HandlerMapping,vievResolver,interceper -->
<!-- 默认映射处理器
<beanclass="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
简单URL的映射处理器
<beanclass="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mapping">
<props>
<propkey="/hello1.do">testController</prop>
</props>
</property>
</bean>
-->
<!-- 先配置controller,使用默认的映射处理器处理Controller-->
<bean id="testcontroller" name="/hello.do" class="cn.itcast.springmvc.TestController"></bean>
<!--视图解释器,前缀+viewName+后缀:从webroot到某一指定的文件夹 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图的前缀 -->
<property name="prefix"value="/WEB-INF/jsp/"></property>
<!--视图的后缀 -->
<property name="suffix"value=".jsp"></property>
</bean>
</beans>
Index.jsp:
<%@ page language="java"contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hello springmvc ,good to see you!
</body>
</html>
TestController.java:
package cn.itcast.springmvc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
importorg.springframework.web.servlet.mvc.AbstractController;
public class TestController extends AbstractController {
@Override
protectedModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponsearg1) throws Exception {
System.out.println("hellospringmvc");
returnnew ModelAndView("index");
}
}
显示结果: