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

springmvc框架的搭建

2015-03-01 14:51 295 查看
1引入jar包

jar包下载地址http://maven.springframework.org/release/org/

以下是我引入的jar包

aopalliance-1.0.jar
aspectjrt.jar
aspectjweaver.jar
commons-beanutils-1.8.0.jar
commons-codec-1.6.jar
commons-collections-3.1.jar
commons-dbcp-1.2.1.jar
commons-fileupload-1.2.1.jar
commons-io-2.0.1.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
commons-logging-1.1.3.jar
commons-net-2.2.jar
commons-pool-1.2.jar
freemarker-2.3.19.jar
hamcrest-all-1.3.jar
httpclient-4.4-beta1.jar
jackson-core-asl-1.9.12.jar
jackson-mapper-asl-1.9.12.jar
json-20090211.jar
json-lib-2.4-jdk15.jar
jsoup-1.7.2.jar
junit-4.11.jar
log4j-1.2.16.jar
mysql-connector-java-5.0.8-bin.jar
spring-aop-3.2.9.RELEASE.jar
spring-aspects-3.2.9.RELEASE.jar
spring-beans-3.2.9.RELEASE.jar
spring-context-3.2.9.RELEASE.jar
spring-context-support-3.2.9.RELEASE.jar
spring-core-3.2.9.RELEASE.jar
spring-expression-3.2.9.RELEASE.jar
spring-framework-bom-3.2.9.RELEASE.jar
spring-instrument-3.2.9.RELEASE.jar
spring-jdbc-3.2.9.RELEASE.jar
spring-orm-3.2.9.RELEASE.jar
spring-oxm-3.2.9.RELEASE.jar
spring-test-3.2.9.RELEASE.jar
spring-tx-3.2.9.RELEASE.jar
spring-web-3.2.9.RELEASE.jar
spring-webmvc-3.2.9.RELEASE.jar
struts2-json-plugin-2.3.15.1.jar
xmlschema-core-2.0.jar
xstream-1.3.1.jar

2导入log4j文件(自己去网上找,不用也没关系,主要是为了进行检测项目的运行状态,进行报错处理)

3在src 目录下写一个applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
<description>Spring-初始 </description>

<!-- //开启mvc注解模式--->

<mvc:annotation-driven />

<!---//扫包() (web层的注解不在这个文件扫描) -->

<context:component-scan:base-package="包名">

<context:component-scan:base-package="包名">

<!---创建对象实例 相当于Object obj = new Object() , obj.prop1 = ""....;整个容器中只存一个实例,从而大大降低了内存的消耗。这里以配置一个数据源为例--->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >

<property name="driverClassName" value="com.jdbc.mysql.Driver"></property>

<property name="url" value="jdbc:mysql://localhost:3306/test"></property>

<property name ="username" value="root"></property>

<property name="password" value="password"></property>

</bean>

</beans>

4.在WEB-INF目录下新建一个xml文件 格式为 项目名-servlet.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:p="http://www.springframework.org/schema/p"
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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
扫描web包,应用Spring的注解
*代表是类
**类和包
Annotation-specified bean name 'userController'
for bean class [com.tz.web.user.UserController]
conflicts with existing, non-compatible bean definition of same name and class
[com.tz.web.UserController
-->

<!--扫描web层-->
<context:component-scan base-package="com.zd.web.**"/>

<!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->

<bean class="org.springframework.web.servlet.view.InternalResoureViewResolver"

p:viewClass="org.springframework.web.servlet.view.JstlView"

p:prefix="/WEB-INF/jsp/"

p:suffix=".jsp"

/>

<!-- springmvc 资源文件管理,异常处理,拦截器,数据类型转换,视频,ajax,文件上传,验证码,路径的说明,参数传递和解说. -->

</beans>

5配置web.xml

?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>SpringFirst</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 监听上下文每一个实例的变化 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--servlet-mapping -->
<servlet>
<servlet-name>SpringFirst</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringFirst</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
</web-app>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: