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

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

程序员文章站 2023-12-05 20:03:58
工具篇:intellij idea+maven+spring+springmvc spring+springmvc环境搭建 一、springmvc环境搭...
  • 工具篇:intellij idea+maven+spring+springmvc
  • spring+springmvc环境搭建

一、springmvc环境搭建

1、创建新项目

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

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

(2)、第二步是填写groupid 和artifactid

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

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

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

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

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

到这里项目就创建成功了

2、添加maven中项目所需要的jar包

在pom.xml中添加项目springmvc所依赖的jar包

<?xml version="1.0" encoding="utf-8"?>

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelversion>4.0.0</modelversion>

 <groupid>com.zbt</groupid>
 <artifactid>enviroment</artifactid>
 <version>1.0-snapshot</version>
 <packaging>war</packaging>

 <name>enviroment maven webapp</name>
 <!-- fixme change it to the project's website -->
 <url>http://www.example.com</url>

 <properties>
 <project.build.sourceencoding>utf-8</project.build.sourceencoding>
 <maven.compiler.source>1.7</maven.compiler.source>
 <maven.compiler.target>1.7</maven.compiler.target>
 </properties>

 <dependencies>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-aop</artifactid>
  <version>5.0.6.release</version>
 </dependency>

 <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-aspects</artifactid>
  <version>5.0.6.release</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-beans</artifactid>
  <version>5.0.6.release</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-context</artifactid>
  <version>5.0.6.release</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-core</artifactid>
  <version>5.0.6.release</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-expression</artifactid>
  <version>5.0.6.release</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-jdbc</artifactid>
  <version>5.0.6.release</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-orm</artifactid>
  <version>5.0.6.release</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-tx</artifactid>
  <version>5.0.6.release</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-web</artifactid>
  <version>5.0.6.release</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-webmvc</artifactid>
  <version>5.0.6.release</version>
 </dependency>
 <!--servlet相关的jar包-->
 <dependency>
  <groupid>javax.servlet</groupid>
  <artifactid>javax.servlet-api</artifactid>
  <version>3.1.0</version>
  <scope>provided</scope>
 </dependency>

 <dependency>
  <groupid>javax.servlet</groupid>
  <artifactid>jstl</artifactid>
  <version>1.2</version>
 </dependency>

 <dependency>
  <groupid>taglibs</groupid>
  <artifactid>standard</artifactid>
  <version>1.1.2</version>
 </dependency>
 </dependencies>

 <build>
 <finalname>enviroment</finalname>
 </build>
</project>

3、配置web.xml文件

<!doctype web-app public
  "-//sun microsystems, inc.//dtd web application 2.3//en"
  "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee"
   xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
   xsi:schemalocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <!--设置我们的url都会交给dispatcherservlet-->
 <welcome-file-list>
 <welcome-file>index.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-mvc.xml</param-value>
 </init-param>
 <!--tomcat 启动时就会初始化dispatcherservlet-->
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>springmvc</servlet-name>
 <url-pattern>*.do</url-pattern>
 </servlet-mapping>
</web-app>

5、配置spring-mvc.xml文件

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

<?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-2.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 <!--启用spring的一些annotation-->
 <context:annotation-config/>

 <!--自动扫描com.zbt.controller-->
 <context:component-scan base-package="zbt.com.controller"/>

 <!--配置注解驱动可以将request参数绑定到controller参数上-->
 <mvc:annotation-driven/>

 <bean 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>
  <property name="suffix" value=".jsp"></property>
 </bean>

</beans>

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

<html>
<body>
 <h2>hello world!</h2>
</body>
</html>

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

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

显示结果为

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

注意事项

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

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

而不是下面这种形式

<servlet-mapping>
 <servlet-name>springmvc</servlet-name>
 <url-pattern>/*</url-pattern>
</servlet-mapping>

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

 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项目环境搭建的步骤(图文) 

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

<init-param>
  <param-name>contextconfiglocation</param-name>
  <param-value>classpath:/spring-mvc.xml</param-value>
 </init-param>

二、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配置文件

<?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:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemalocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.3.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
 <context:component-scan base-package="com.zbt">
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller"/>
 </context:component-scan>
</beans>

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

<context:component-scan base-package="com.zbt">
 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller"/>
</context:component-scan>

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

<?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-2.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 <!--启用spring的一些annotation-->
 <context:annotation-config/>

 <!--自动扫描com.zbt 而不是com.zbt.controller包-->
 <context:component-scan base-package="com.zbt"/>

 <!--配置注解驱动可以将request参数绑定到controller参数上-->
 <mvc:annotation-driven/>

 <bean 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>
  <property name="suffix" value=".jsp"></property>
 </bean>

</beans>

将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项目环境搭建的步骤(图文)

注意事项

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

<!--错误配置方式-->
<context:component-scan base-package="com.zbt.controller"/>
<!--正确配置方式
<context:component-scan base-package="com.zbt"/>
-->

出现的错误

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中应该配置成扫描所有的包

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