springMVC+annotation的简单配置
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Spring MVC + Annotation
1.创建一个web工程名字spring_mvc
2.添加相应配置文件
添加spring应用上下文的文件applicationContext.xml
Log4j配置文件:log4j.properties
3.在web.xml中添加配置
1.配置spring上下文
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value> </context-param>
2.添加log4J的配置文件
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param>
3.设置字符集
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filt er-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.对spring上下文添加监听
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</lis tener-class>
</listener>
5.Spring转发器
<servlet>
<servlet-name>spring_mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring_mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
6.配置错误转向页面
<error-page>
<error-code>404</error-code>
<location>/404.htm</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.htm</location>
</error-page>
4.applicationContext.xml的配置
导入dao.xml配置文件
<import resource="dao.xml"/>
导入service.xml
<import resource="service.xml"/>
导入数据库连接配置文件
<context:property-placeholder location="/WEB-INF/database.properties"/>
5.dao.xml的配置
对com.alex.web.dao开启注释
<context:component-scan
base-package="com.alex.web.dao"></context:component-scan>
配置dataSource
<bean id="dataSource"
class="mons.dbcp.BasicDataSource"
destroy-method="close"
lazy-init="false"
p:driverClassName="${dataSource.driverClassName}"
p:url="${dataSource.url}"
p:username="${ername}"
p:password="${dataSource.password}"
p:maxActive="${dataSource.maxActive}"
p:maxIdle="${dataSource.maxIdle}"
p:maxWait="${dataSource.maxWait}" />
配置jdbcTemplate 实际是对jdbc的封装
<bean class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource" />
配置事务
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
5.service.xml的配置
对om.alex.web.service开启注释
<context:component-scan
base-package="com.alex.web.service"></context:component-scan>
6.servlet.xml的配置
对com.alex.web.controller开启注释
<context:component-scan
base-package="com.alex.web.controller"></context:component-scan>
Spring的默认解析器
<bean id="urlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandle rMapping"></bean>
根据参数和方法名解析
<bean id="actionMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameRes olver">
<property name="paramName" value="_action" />
<property name="defaultMethodName" value="find" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandler Adapter">
<property name="cacheSeconds" value="0" />
<property name="methodNameResolver" ref="actionMethodResolver" /> </bean>
视图解析器
<bean id="jstlViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/page/"
p:suffix=".jsp" />
7.log4j的配置
log4j.rootLogger=INFO,A1
# \u8f93\u51fa\u5230\u63a7\u5236\u53f0
log4j.appender.A1=org.apache.log4j.ConsoleAppender
yout=org.apache.log4j.PatternLayout
yout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} [\u65E5\u5FD7\u4FE1\u606F]%m%n
8.database.properties的配置
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/springmvc
ername=root
dataSource.password=cpic1234
dataSource.maxActive=200
dataSource.maxIdle=50
dataSource.maxWait=10000。