您的位置:首页 > 编程语言 > Java开发

SpringMVC手动配置

2016-03-25 15:19 471 查看

Spring MVC手动配置

一直以来做项目都再用Spring,但是都不是自己搭建的,虽然配置文件都认识,但也不是都清楚,今天就来手动配置springMVC格式。

首先,导入包这里就不多做赘述了,在web.xml文件里配置加载spring配置文件。<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>myspringmvc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-servlet.xml</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的配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:ApplicationContext.xml</param-value>
  </context-param>
</web-app>

配置spring-servlet配置文件。

<?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/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.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 配置要扫描的包 -->
<context:component-scan base-package="controller"></context:component-scan>
<context:component-scan base-package="service"></context:component-scan>
<context:component-scan base-package="dao"></context:component-scan>

<!--配置view层 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>配置说明,<context:component-scan>标签的作用就是配置扫描包,spring会扫描该包下所有的Java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean。
viewResolver表示把view文件下面所有jsp文件作为MVC中view层。

网页文件结构如下:

1.controller层

配置如下package controller;

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

import service.HomeService;

@Controller
@RequestMapping("home")
public class HomeController {
    
    @Autowired
    @Qualifier("homeService")
    private HomeService homeService;
    
    @RequestMapping("homePage")
    public String homePage(){
        
        homeService.home();
        return "homePage";
    }
}


@Controller 负责注册一个bean 到spring 上下文中
@RequestMapping 注解为控制器指定可以处理哪些 URL 请求

2.service层

package service;

import org.springframework.stereotype.Service;

@Service("homeService")
public class HomeService {

public void home(){
System.out.println("homeService调用成功!");
}
}

dao层一般与数据库打交道,而这次我们只配置SpringMVC的配置,在这篇就不介绍数据库配置和dao层
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息