基于动态WEB项目的SpringMVC搭建 非MAVEN

  初次写博 有问题请大家多指教!

用SpringMVC有一段时间,但一直不会搭建,今天正好有时间,重新了解下SpringMVC框架的搭建

1.建立一个 Dynamic Web Project   命名为Test

2.Finish后给项目添加所依赖的包,因为考虑到要做文件上传和JSON的数组传递所以将commons-fileupload-1.2.2.jar,和jackson-core-asl-1.9.11.jar
jackson-mapper-asl-1.9.11.jar两个包一起导入lib文件夹下。

3.在web.xml文件中进行配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     id="WebApp_ID" version="2.5">
 6     <display-name>TestDemo</display-name>
 7     <context-param>
 8         <param-name>contextConfigLocation</param-name>
 9         <param-value>
10             classpath*:/applicationContext.xml
11         </param-value>
12     </context-param>
13     <!-- 定义filter -->
14     <!-- Reads request input using UTF-8 encoding -->
15     <filter>
16         <filter-name>encodingFilter</filter-name>
17         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
18         <init-param>
19             <param-name>encoding</param-name>
20             <param-value>UTF-8</param-value>
21         </init-param>
22         <init-param>
23             <param-name>forceEncoding</param-name>
24             <param-value>true</param-value>
25         </init-param>
26     </filter>
27     <filter-mapping>
28         <filter-name>encodingFilter</filter-name>
29         <url-pattern>/*</url-pattern>
30     </filter-mapping>
31     <listener>
32         <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
33     </listener>
34     <listener>
35         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
36     </listener>
37     <listener>
38         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
39     </listener>
40     <servlet>
41         <servlet-name>dispatcher</servlet-name>
42         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
43         <load-on-startup>1</load-on-startup>
44     </servlet>
45     <servlet-mapping>
46         <servlet-name>dispatcher</servlet-name>
47         <url-pattern>*.do</url-pattern>
48     </servlet-mapping>
49     <welcome-file-list>
50         <welcome-file>index.jsp</welcome-file>
51     </welcome-file-list>
52 </web-app>
View Code


4.在WebContent下建立index.jsp文件,在WEB-INF中建立pages文件夹,并在WEB-INF文件夹下建立dispatcher-servlet.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:p="http://www.springframework.org/schema/p"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 9                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
10                         http://www.springframework.org/schema/context 
11                         http://www.springframework.org/schema/context/spring-context-3.1.xsd
12                         http://www.springframework.org/schema/tx 
13                         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
14                         http://www.springframework.org/schema/mvc 
15                         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
16 
17 
18 <!-- 对指定包中的所有类进行扫描,以完成bean创建和自动依赖注入的功能 -->
19     <context:component-scan base-package="/" />
20 
21     <!-- 配置视图解析 -->
22     <bean id="viewResolver"
23         class="org.springframework.web.servlet.view.InternalResourceViewResolver"
24         p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
25     <!-- 配置文件上传 -->
26     <bean id="multipartResolver"
27         class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
28         <property name="defaultEncoding" value="UTF-8"></property>          
29     </bean>
30     <!-- 配置json数组转换 -->
31     <bean
32         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
33         <property name="messageConverters">
34             <list>
35                 <ref bean="mappingJacksonHttpMessageConverter" />
36             </list>
37         </property>
38     </bean>
39 
40     <bean id="mappingJacksonHttpMessageConverter"
41         class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
42     
43 </beans>
View Code

5.在src下建立applicationContext.xml文件进行如下配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:jee="http://www.springframework.org/schema/jee" 
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 8         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 9         http://www.springframework.org/schema/context 
10         http://www.springframework.org/schema/context/spring-context-3.1.xsd
11         http://www.springframework.org/schema/jee 
12         http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
13         http://www.springframework.org/schema/tx  
14         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
15 
16     <context:component-scan base-package="/" />
17     
18     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
19         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
20         <property name="url" value="jdbc:mysql://localhost:3306/dbname"/>
21         <property name="initialSize" value="5"/>
22         <property name="maxActive" value="20"/>
23         <property name="defaultAutoCommit" value="true"/>
24         <property name="username" value="username"/>
25         <property name="password" value="password"/>    
26     </bean>
27     
28     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
29         <property name="dataSource" ref="dataSource"/>
30     </bean>
31     
32     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" depends-on="dataSource">
33         <constructor-arg type="DataSource" ref="dataSource" /> 
34     </bean>
35     
36 </beans>
View Code

6.在src下建立test包 在保重建立TestController类 代码如下:

 1 package test;
 2 import org.springframework.stereotype.Controller;
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 
 5 @Controller
 6 public class TestController {
 7 
 8     @RequestMapping("/testCas.do")
 9     public String testCas(){
10         return "/success";
11     }
12 }
View Code

7.在index.jsp中写入如下呆代码:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Test</title>
 8 </head>
 9 <body>
10 <a href="testCas.do">test</a>
11 </body>
12 </html>
View Code

8.在pages文件夹下建立success.jsp文件
9.测试 将项目部署进Tomcat中后启动服务,在浏览器地址栏中输入http://localhost:8080/Test   

10.点击test跳转success页面

posted @ 2013-06-07 16:45  流浪的石头  阅读(531)  评论(0)    收藏  举报