配置spring

1.把spring的jar文件放进WEB-INF/lib文件夹中

2.在web.xml文件中添加以下内容

添加spring前端控制器

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

添加控制器映射

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

建立spring-servlet.xml文件放置在WEB-INF目录下 文件名是根据你起的servlet名字来的 像这里servlet名为spring那么这个映射文件就叫spring-servlet.xml

以上是spring mvc需要添加的内容

要使用spring还需要添加以下内容

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

把spring配置文件applicationContext.xml放进WEB-INF/classes文件夹中

另外记得下载commons-logging-1.1.1.jar文件并放置在WEB-INF/lib目录中

要使用ApplicationContext的话,在源代码中这样写

ServletContext sc = request.getServletContext();
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sc);
 
UserService service = (UserService) context.getBean("userService");

猜你喜欢

转载自civili.iteye.com/blog/1255199