几个注解的区别:
1 @WebServlet(urlPatterns="/android/login") //相当于web.xml里写servlet-mapping
public class LoginServlet extends BaseServlet{
}
2.@Controller("/android/login") @RequestMapping
//SPRING的注解,加入到spring控制,这样<context:component-scan就可以扫瞄到,当然其里面的@Resource 变量也就可以注 入 ,
访问时 http://localhost:8080/GoDutchServer/android/login?user=jimmy&pass=jimmy 或者android/login.do都可
如果是@Controller("/android/login.jsp") 则路径是/android/login.jsp.do
public class LoginServlet extends BaseServlet{
@Resource UserService userService;
//这个@RequestMapping一定要写,否则路径找不到,(不知道是不是spring版本问题)
@RequestMapping(method = RequestMethod.GET)
public void doGet(HttpServletRequest request ,
HttpServletResponse response)
throws IOException, ServletException
{
service(request , response);
}
@RequestMapping(method = RequestMethod.POST)
{}
}
注意如果是@WebServle 则@Resource注解不会被扫描到,当然不会注入变量,教训啊!!),见下图 报 can not create resource
3. web.xml
<display-name>godutch</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化,第一道配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 对SpringMVC容器进行实例化,不一样啊,两道配置哦 ,拦截路径的请求-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
4.beans.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.jimmy.godutch" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> //其实不写这行也没关系,不信你试试
</context:component-scan>
<!-- 使用数据源和指定persistence.xml位置的方式创建entityManagerFactory, 注意:使用该方式需要把persistence.xml中的hibernate.connection.driver_class,hibernate.connection.username,hibernate.connection.password,hibernate.connection.url配置删除
-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"/>
<!-- 连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="1"/>
<!-- 连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300"/>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5"/>
<!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="ds"/> //数据源
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> //jpa
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
5.persistence.XML
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!-- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="123456" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/godutch" />
-->
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.jdbc.fetch_size" value="18"/>
<property name="hibernate.jdbc.batch_size" value="10"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
6.jdbc.properties
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/godutch?useUnicode=true&characterEncoding=UTF-8
user=root
password=123456
7.spring-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<!-- scan all controller -->
<context:component-scan base-package="com.jimmy.godutch.servlet" use-default-filters="false" >
<!-- spring mvc配置文件中注解只扫controller注解 -总要,不配可能找不到路径 ,404错误->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- access the static resource, one of two -->
<!-- <mvc:default-servlet-handler /> -->
<!-- access the static resource, one of two -->
<mvc:resources mapping="/images/**" location="/images/"cache-period="31556926" />
<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926" />
<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926" />
<mvc:resources mapping="/index.html" location="/index.html" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".html" />
<!-- <property name="viewClass" value="org.springframework.web.servlet.view.Jstlview" /> -->
</bean>
<mvc:default-servlet-handler/>
<!-- 启动Spring MVC的注解功能 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
</bean>
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
</beans>