欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Spring in action】基于xml配置:在SpringMVC中使用Spring容器

程序员文章站 2024-02-15 12:01:05
...

前言:

可能作者的层次较高,并没有考虑到SpringMVC+Spring的配置还有新人自学不知道吧。捂脸!

正文:

一、配置整合springmvc+spring:

1、在src也就是classpath下新建spring-serlvet.xml配置文件,用来配置spring容器的bean:扫描model文件夹下所有。

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<context:component-scan base-package="model"></context:component-scan>  
</beans>
2、在WEB-INF下配置spring-servlet.xml,配置springmvc

<?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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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-4.3.xsd">
	<!-- 配置handlerMapping -->
	<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean>
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 :说明如果直接放在web-inf下,不需要加前缀-->
		<property name="prefix" value="WEB-INF/view/"></property>
		<!-- 后缀 : -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!-- 加入注解驱动 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 扫描器 -->
	<context:component-scan base-package="com.springmvc.controller"></context:component-scan>
</beans>
3、在WEB-INF下配置web.xml,将spring+springmvc整合起来。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>SpringMVC2</display-name>
	<!-- 配置Spring1:配置spring-servlet.xml路劲 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-servlet.xml</param-value>
	</context-param>
	<!-- 配置Spring2:配置Spring监听2 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 创建分发Servlet -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>

二、测试springmvc+spring:

1、在src下新建model文件夹,新建user对象:

package model;

import java.io.Serializable;

public class User implements Serializable{
	private int userId;
	private String name;
	public User(int i, String string) {
		this.userId = i;
		this.name = string;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "User [userId=" + userId + ", name=" + name + "]";
	}
}
2、在model文件夹下新建接口:为什么用接口?因为此处可以通过接口实现自动装配。

package model;

public interface IUserService {
	User getUserById(int userId); 
}
3、在model下写一个接口的实现类:此处可写操作数据库的方法等操作或者业务逻辑。

package model;

import org.springframework.stereotype.Component;

@Component 
public class UserServiceImpl implements IUserService {
	
    @Override  
    public User getUserById(int userId) {  
        return new User(1,"xionghaibo");  
    }  

}

4、写一个index.jsp用来网页请求返回展示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>${helloworld}</h1>
</body>
</html>
5、整合上述所有步骤。在src下新建文件夹com.springmvc.controller,在文件夹下新建controller控制器helloController:

package com.springmvc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import model.IUserService;

@Controller
public class HelloController {
	@Autowired
	IUserService IUserService;
	//返回到index.jsp	
	@RequestMapping(value="/hello.do")
	public String hello(String userName,Model model){
		System.out.println(userName);
		model.addAttribute("helloworld","hello"+IUserService.getUserById(1));
		return "index";
	}
}
6、目录结构一览:

【Spring in action】基于xml配置:在SpringMVC中使用Spring容器

7、测试结果一览:

【Spring in action】基于xml配置:在SpringMVC中使用Spring容器