JavaEE体系结构包括四层,从上到下分别是应用层、Web层、业务层、持久层。Struts和SpringMVC是Web层的框架,Spring是业务层的框架,Hibernate和MyBatis是持久层的框架。[XML] 纯文本查看 复制代码 web.xml
<web-app>
<servlet>
<!-- 加载前端控制器 -->
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
加载配置文件
默认加载规范:
* 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml
* 路径规范:必须在WEB-INF目录下面
修改加载路径:
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
[XML] 纯文本查看 复制代码 springmvc.xml
<beans>
<!-- 配置映射处理器:根据bean(自定义Controller)的name属性的url去寻找handler;springmvc默认的映射处理器是
BeanNameUrlHandlerMapping
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 配置处理器适配器来执行Controlelr ,springmvc默认的是
SimpleControllerHandlerAdapter
-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 配置自定义Controller -->
<bean id="myController" name="/hello.do" class="org.controller.MyController"></bean>
<!-- 配置sprigmvc视图解析器:解析逻辑试图;
后台返回逻辑试图:index
视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
(3)自定义处理器
public class MyController implements Controller{
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
ModelAndView mv = new ModelAndView();
//设置页面回显数据
mv.addObject("hello", "欢迎学习springmvc!");
//返回物理视图
//mv.setViewName("/WEB-INF/jsps/index.jsp");
//返回逻辑视图
mv.setViewName("index");
return mv;
}
}
|