SpringMVC是一个被广泛应用的基础框架,如何应用它是程序员们必备的技能。文章的着重点在讲如何应用,对于原理感兴趣的可以自己去参考其他的文章。
自定义配置文件的路径
要搭建SpringMVC环境,需要配置两个配置文件其一是web.xml,另一个就是SpringMVC相关的bean.xml文件。SpringMVC的bean.xml配置文件用户可以自定义路径,不过需要在web.xml告诉Spring初始化时候从哪去寻找bean.xml。下面看看如何在web.xml自定义文件路径
<!-- 配置Spring DispatcherServlet -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring/springMVC.xml</param-value>
<!-- <param-value>/WEB-INF/root-context.xml</param-value> -->
</context-param>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
通过上面的配置告诉Spring在初始化的时候去classpath路径下conf/spring/目录中寻找springMVC.xml文件,SpringMVC相关的配置在这里进行配置。需要指出的[url-pattern]最好采用"/",使用"/*"会造成一下小麻烦。下面看如何配置第一个SpringMVC的Controller
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 根据 Bean的name来映射路径-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 根路径/ 不可省略 否则找不到对象 -->
<bean name="/helloWorld" class="org.lian.controller.HelloWorldController"/>
</beans>
需要指出上面配置一个bean对象jspViewResolver,这个会告诉SpringMVC视图层是*.jsp文件,并且这些jsp文件都放在WEB-INF/jsp/**/目录下面。定义HelloWorldController,这个类需要实现Controller接口。
public class HelloWorldController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
return mv;
}
}
在浏览器中输入项目的根路径/helloWorld,SpringMVC会找到WEB-INF/jsp/index.jsp并返回给浏览器。
本文介绍如何搭建SpringMVC环境,包括配置web.xml和bean.xml文件以自定义配置路径,以及配置第一个Controller。
2万+

被折叠的 条评论
为什么被折叠?



