Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文)

  • 工具篇:Intellij Idea+maven+Spring+SpringMVC
  • Spring+SpringMVC环境搭建

一、SpringMVC环境搭建

1、创建新项目

(1)、第一步是创建一个由Maven原型的项目,根据图片上的步骤一次选择Maven―�C>create from archetype―->maven-archtype-webapp

Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文)_第1张图片 

(2)、第二步是填写GroupId 和ArtifactId

Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文)_第2张图片 

(3)、在位置1处选择我们maven安装的目录,在位置2处选择settings.xml的路径(我只是设置了一次之后就是默认的了)

Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文)_第3张图片 

(4)、设置项目名和项目的位置

Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文)_第4张图片 

到这里项目就创建成功了

2、添加Maven中项目所需要的Jar包

在pom.xml中添加项目SpringMVC所依赖的Jar包




 4.0.0

 com.zbt
 enviroment
 1.0-SNAPSHOT
 war

 enviroment Maven Webapp
 
 http://www.example.com

 
 UTF-8
 1.7
 1.7
 

 
 
 
  org.springframework
  spring-aop
  5.0.6.RELEASE
 

 
 
  org.springframework
  spring-aspects
  5.0.6.RELEASE
 
 
 
  org.springframework
  spring-beans
  5.0.6.RELEASE
 
 
 
  org.springframework
  spring-context
  5.0.6.RELEASE
 
 
 
  org.springframework
  spring-core
  5.0.6.RELEASE
 
 
 
  org.springframework
  spring-expression
  5.0.6.RELEASE
 
 
 
  org.springframework
  spring-jdbc
  5.0.6.RELEASE
 
 
 
  org.springframework
  spring-orm
  5.0.6.RELEASE
 
 
 
  org.springframework
  spring-tx
  5.0.6.RELEASE
 
 
 
  org.springframework
  spring-web
  5.0.6.RELEASE
 
 
 
  org.springframework
  spring-webmvc
  5.0.6.RELEASE
 
 
 
  javax.servlet
  javax.servlet-api
  3.1.0
  provided
 

 
  javax.servlet
  jstl
  1.2
 

 
  taglibs
  standard
  1.1.2
 
 

 
 enviroment
 

3、配置web.xml文件




 
 
 index.jsp
 
 
 springMVC
 org.springframework.web.servlet.DispatcherServlet
 
  contextConfigLocation
  classpath:/spring-mvc.xml
 
 
 1
 
 
 springMVC
 *.do
 

5、配置spring-mvc.xml文件

在resources文件夹下新建一个spring-mvc.xml。spring-mvc.xml文件的功能是什么?



 
 

 
 

 
 

 
  
  
  
 

6、编写Controller类

package controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/student")
public class StudentController {
 private static final Log logger = LogFactory.getLog(StudentController.class);
 @RequestMapping("/hello")
 public String sayHi(){
  logger.info("sayHi are called!");
  return "hello";
 }
}

在WEB-INF/jsp文件夹下创建测试的hello.jsp



 

Hello World!

启动服务器,在浏览器中输入

http://localhost:8088/student/hello.do

显示结果为

注意事项

(1)、url-pattern最好配置成下面的形式


 springMVC
 *.do

而不是下面这种形式


 springMVC
 /*

配置成第二种模式会出现下面的情况

 27-Jun-2018 15:04:02.761 警告 [http-nio-8088-exec-8] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'springMVC'

网页会出现如下情况

Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文)_第5张图片 

(2)、我们的spring-mvc.xml是放在resources文件夹下面的,以后就按照这种方式编写(注意这是在Intellij idea中,在Eclipse中我不知道会不是是一样的)


  contextConfigLocation
  classpath:/spring-mvc.xml
 

二、Spring+SpringMVC环境搭建

编写StudentService

package com.zbt.service;

import org.springframework.stereotype.Service;

/**
 * Created by luckyboy on 2018/7/2.
 */
@Service
public class StudentService {
 public String test(){
  return "hello";
 }
}

创建applicationConfig配置文件



 
  
 

注意Spring这里的包扫描方式为,Spring扫描包的时候会排出@Controller注解,也就是不会扫描com.zbt.controller包


 

修改spring-mvc.xml中的包扫描方式



 
 

 
 

 
 

 
  
  
  
 

将StudentService注入到StudentController中

package com.zbt.controller;

import com.zbt.service.StudentService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by luckyboy on 2018/6/27.
 */
@Controller
@RequestMapping("/student")

 private static final Log logger = LogFactory.getLog(StudentController.class);

 //使用Autowired将StudentService注入到StudentController中
 @Autowired
 private StudentService stuService;


 @RequestMapping("/hello.do")
 public String sayHi(){
  logger.info("sayHi are called!");
  return "hello";
 }

 @RequestMapping("/spring_hello.do")
 public String springSayHi(){
  logger.info("sayHi are called by springSayHi");
  return stuService.test();
 }

}

在StudentService类上面添加@Service注解

package com.zbt.service;

import org.springframework.stereotype.Service;

/**
 * Created by luckyboy on 2018/7/2.
 */
@Service
public class StudentService {
 public String test(){
  return "hello";
 }
}

测试

启动服务器,在浏览器地址栏输入http://localhost:8088/student/spring_hello.do,响应如下

Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文)_第6张图片

注意事项

我们在spring-mvc.xml中的配置包扫描时,如果包扫描配置如下



出现的错误

org.springframework.web.servlet.FrameworkServlet.initServletBean Context initialization failed
 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'stuService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zbt.service.StudentService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)
 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1348)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
 at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:672)
 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:638)
 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:686)
 at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:554)
 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:499)
 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:172)
 at javax.servlet.GenericServlet.init(GenericServlet.java:158)
 at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1144)
 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091)
 at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:983)
 at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4939)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5249)
 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
 at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1736)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
 at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
 at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
 at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482)
 at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
 at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
 at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
 at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
 at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
 at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
 at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
 at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
 at sun.rmi.transport.Transport$1.run(Transport.java:200)
 at sun.rmi.transport.Transport$1.run(Transport.java:197)
 at java.security.AccessController.doPrivileged(Native Method)
 at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
 at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
 at java.security.AccessController.doPrivileged(Native Method)
 at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zbt.service.StudentService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)
 ... 67 more

会出现包无法扫描的情况,因此我们在spring-mvc.xml中应该配置成扫描所有的包

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Spring+SpringMVC+Hibernate项目环境搭建的步骤(图文))