Spring MVC 的配置安装
这个是李绪成老师今天布置的作业,我顺便做了个教程欢迎大家交流。首先祝大家女神节快乐!(2016,3,8)
一、首先你要准备Eclipse开发环境:
至于Jdk的环境变量请自行百度、谷歌,里面的教程非常详细。
Eclipse软件请自行去百度或谷歌下载!配好Eclipse环境就该我们大显身手了!请继续往下看,,,(作为程序员这个都还不会,你想干啥?你咋不飞呢!)
1.打开Eclipse,点击File-->new,弹出窗口选择Web-->Dynamic web project.
2.一直next,在新窗口中输入项目名称,然后继续next,最后finish。项目列表中就有你输入名称的项目了!
3.导入Spring MVC相关的JAR包到lib下,首先下载jar包。
解压缩后直接将jar文件复制粘贴到WebContent目录下的WEB-INF目录里的lib目录里。
4.配置项目引用刚才导入到lib下的jar包。
右键点击项目名,在菜单中选择Build Path-->Configuration build path...
二、新增Spring MVC的配置文件:
找到项目目录下的Java Resources,右键点击Java Resources,在弹出窗口选择new-->source folder.
source folder建好之后,需要把配置好文件复制粘贴进去
这四个文件的说明:
database.properties:配置数据库连接的地址,用户名以及密码
1 mysql.driverclass=com.mysql.jdbc.Driver 2 mysql.jdbcurl=jdbc:mysql://127.0.0.1:3306/shujuku_name?autoReconnect=true&useUnicode=true&characterEncoding=utf8 3 mysql.user=root 4 mysql.password=abc123
jdbc-context.xml:spring mvc的jdbc连接数据库的bean的配置
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:p="http://www.springframework.org/schema/p" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:context="http://www.springframework.org/schema/context" 8 xmlns:cache="http://www.springframework.org/schema/cache" 9 xsi:schemaLocation=" 10 http://www.springframework.org/schema/beans 11 http://www.springframework.org/schema/beans/spring-beans-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/context 15 http://www.springframework.org/schema/context/spring-context-3.1.xsd 16 http://www.springframework.org/schema/aop 17 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd" default-autowire="byName"> 18 19 <!-- 创建数据源 commons-dbcp.jar; commons-pool.jar包--> 20 <context:property-placeholder location="classpath:database.properties"/> 21 <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> 22 <property name="driverClassName" value="${mysql.driverclass}" /> 23 <property name="url" value="${mysql.jdbcurl}" /> 24 <property name="username" value="${mysql.user}" /> 25 <property name="password" value="${mysql.password}" /> 26 </bean> 27 28 <bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"> 29 <constructor-arg><ref bean="dataSource"/></constructor-arg> 30 </bean> 31 32 <!-- 看了spring源码,该 org.springframework.jdbc.core.JdbcTemplate类的继承类有dataSource属性,所以可以属性注入--> 33 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 34 <property name="dataSource"> 35 <ref bean="dataSource"/> 36 </property> 37 </bean> 38 39 </beans>
springMvc3-servlet.xml:spring mvc的其他配置(非常重要)
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" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation=" 7 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/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 13 14 <!-- 启动扫描所有的controller --> 15 <context:component-scan base-package="com.codingyun.core.action"/> 16 17 <!-- 主要作用于@Controller,激活该模式 18 19 下面是一种简写形式,完全可以手动配置替代这种简写形式; 20 它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean, 21 是spring MVC为@Controllers分发请求所必须的 22 --> 23 <!-- <mvc:annotation-driven/> --> 24 <mvc:annotation-driven> 25 <mvc:message-converters register-defaults="true"> 26 <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 27 <constructor-arg value="UTF-8" /> 28 </bean> 29 </mvc:message-converters> 30 </mvc:annotation-driven> 31 32 33 <!-- 配置js,css等静态文件直接映射到对应的文件夹,不被DispatcherServlet处理 --> 34 <mvc:resources location="/WEB-INF/resources/**" mapping="/resources"/> 35 <mvc:resources mapping="/javascript/**" location="/static_resources/javascript/"/> 36 <mvc:resources mapping="/styles/**" location="/static/css/"/> 37 <mvc:resources mapping="/images/**" location="/static/images/"/> 38 39 <!-- 配置页面访问地址www.xxx.com/about返回的静态html文件 --> 40 <mvc:resources mapping="/about/**" location="/WEB-INF/html/"/> 41 <!-- 走servlet的默认配置,先走默认的web.xml配置的servlet,没有的话才找对应controller --> 42 <mvc:default-servlet-handler /> 43 44 <!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在jsp/目录下,查找XXX.jsp文件--> 45 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 46 <property name="prefix" value="/WEB-INF/jsp/"></property> 47 <property name="suffix" value=".jsp"></property> 48 </bean> 49 50 <bean id="multipartResolver" 51 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 52 <property name="defaultEncoding"> 53 <value>UTF-8</value> 54 </property> 55 <property name="maxUploadSize"> 56 <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 --> 57 </property> 58 <property name="maxInMemorySize"> 59 <value>4096</value> 60 </property> 61 </bean> 62 63 </beans>
log4j.properties:控制台打印日志的配置、输出日志文件的配置
1 log4j.rootLogger=INFO,console,R 2 3 log4j.appender.console=org.apache.log4j.ConsoleAppender 4 log4j.appender.console.Target=System.out 5 log4j.appender.console.layout=org.apache.log4j.PatternLayout 6 log4j.appender.console.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n 7 log4j.appender.console.threshold=INFO 8 9 log4j.appender.R=org.apache.log4j.RollingFileAppender 10 log4j.appender.R.layout=org.apache.log4j.PatternLayout 11 log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n 12 log4j.appender.R.File=/alidata/zkyj/web/zkyj-web.log 13 log4j.appender.R.Append=true 14 log4j.appender.R.Threshold=INFO 15 log4j.appender.R.MaxFileSize=20MB 16 log4j.appender.LOGFILE.MaxBackupIndex=100 17 log4j.org.springframework.web=INFO
三、配置web.xml,以及新建jsp、css、js等目录
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" 4 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"> 5 <display-name>zkyj-web</display-name> 6 7 <!-- web.xml 的加载顺序是:context-param -> listener -> filter -> servlet , 8 而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的 --> 9 10 <!-- spring上下文载入监听器,确保web服务启动时,spring已经完成初始化 --> 11 <listener> 12 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 13 </listener> 14 15 <context-param> 16 <param-name>contextConfigLocation</param-name> 17 <param-value>classpath:jdbc-context.xml</param-value> 18 </context-param> 19 20 <filter> 21 <filter-name>encodingFilter</filter-name> 22 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 23 <init-param> 24 <param-name>encoding</param-name> 25 <param-value>UTF-8</param-value> 26 </init-param> 27 <init-param> 28 <param-name>ForceEncoding</param-name> 29 <param-value>true</param-value> 30 </init-param> 31 </filter> 32 33 <filter-mapping> 34 <filter-name>encodingFilter</filter-name> 35 <url-pattern>/*</url-pattern> 36 </filter-mapping> 37 38 <servlet> 39 <servlet-name>springMvc3</servlet-name> 40 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 41 <init-param> 42 <param-name>contextConfigLocation</param-name> 43 <param-value>classpath:springMvc3-servlet.xml</param-value> 44 </init-param> 45 <load-on-startup>1</load-on-startup> 46 </servlet> 47 48 <!-- 会话超时配置(单位为分钟) --> 49 <session-config> 50 <session-timeout>120</session-timeout> 51 </session-config> 52 53 54 <servlet-mapping> 55 <servlet-name>default</servlet-name> 56 <url-pattern>*.jpg</url-pattern> 57 </servlet-mapping> 58 <servlet-mapping> 59 <servlet-name>default</servlet-name> 60 <url-pattern>*.js</url-pattern> 61 </servlet-mapping> 62 <servlet-mapping> 63 <servlet-name>default</servlet-name> 64 <url-pattern>*.css</url-pattern> 65 </servlet-mapping> 66 <servlet-mapping> 67 <servlet-name>springMvc3</servlet-name> 68 <url-pattern>/</url-pattern> 69 </servlet-mapping> 70 <welcome-file-list> 71 <welcome-file>index.html</welcome-file> 72 </welcome-file-list> 73 74 <!-- 通过错误码来配置error-page ,配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp。 --> 75 <error-page> 76 <error-code>404</error-code> 77 <location>/page404.html</location> 78 </error-page> 79 <!-- 通过异常的类型配置error-page ,配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp --> 80 <error-page> 81 <exception-type>java.lang.NullPointerException</exception-type> 82 <location>/WEB-INF/jsp/error.jsp</location> 83 </error-page> 84 85 </web-app>
接下来你就可以大显身手,随你想怎么弄,,,
程序员交流群:297473401