网上关于此教程各种版本,太多太多了,因为我之前没搭过框架,最近带着两个实习生,为了帮他们搭框架,我只好。。。惭愧啊。。。基本原理的话各位自己了解下,表示我自己从来没研究过Spring的源码,所以工作了一年多还是在写代码。。。
下面直接正题,怎么搭建
我的Project目录结构,jsp在web-inf目录,js之类的在webroot根目录。
1.配置web.xml
SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是DispatcherServlet,DispatcherServlet负责转发每一个Request请求给相应的Handler,Handler处理以后再返回相应的视图(View)和模型(Model),返回的视图和模型都可以不指定,即可以只返回Model或只返回View或都不返回。DispatcherServlet是继承自HttpServlet的,既然SpringMVC是基于DispatcherServlet的,那么我们先来配置一下DispatcherServlet。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>sy</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/main/config/applicationContext.xml,classpath*:/main/config/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.配置springmvc-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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 对web包中的Controller进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="main.java.com.sy.controller"/> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!--对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/pages/" p:suffix=".jsp"/> </beans>
3.配置applicationContext.xml,扫描entity,service,dao的注解,其他的就不需要扫描,解析数据库properties文件,配置事务,和之前的Spring配置一样
<?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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.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
"> <context:component-scan base-package="main.java.com.sy.service"></context:component-scan>
<context:component-scan base-package="main.java.com.sy.dao"></context:component-scan>
<context:component-scan base-package="main.java.com.sy.bean"></context:component-scan> <context:property-placeholder location="classpath*:/main/config/application.properties" />
<!-- 支持aop注解 -->
<aop:aspectj-autoproxy /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
<property name="packagesToScan">
<value>main.java.com.sy.entity</value>
</property>
</bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 配置事务管理 -->
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<aop:config>
<aop:pointcut expression="execution(* main.java.com.sy.service.impl.*.*(..))" id="businessService"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />
</aop:config> <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delet*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="create*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="merge*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="revoke*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice> </beans>
4.配置application.properties
#jdbc
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sy
jdbc.user=root
jdbc.password=root #hibernate
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
5.默认是进index.jsp页面,但是我们不可以直接访问,所以呢,做点修改。
<html>
<head>
</head>
<body>
<script type="text/javascript">
top.window.location.replace("login.html");
</script>
</body>
</html>
6.如何才能进入这个页面呢,我们来写个Controller方法
UserController.java
@Controller
public class UserController { @RequestMapping(value = "/login.html")
public String login(HttpServletRequest request,HttpServletResponse reponse) throws Exception{
return "user/login";
}
}
7.因为我们想默认进一个登录页面,方法有了跳转,页面建立一下。
<form action="success.html" name="loginform" accept-charset="utf-8" id="login_form" class="loginForm" method="post">
<label class="input-tips" for="u">帐号:</label>
<input type="text" id="accounName" name="accounName" class="inputstyle" placeholder="输入用户名/登录名"/>
<label class="input-tips" for="p">密码:</label>
<input type="password" id="loginPassword" name="loginPassword" class="inputstyle" placeholder="输入密码" autocomplete="off"/>
<input type="button" id="login_button" value="登 录" style="width:150px;" class="button_blue"/>
</form>
8.进入页面之后,我们需要判断正确性咯,在UserController加个success方法。
@RequestMapping(value = "/success.html")
public String success(HttpServletRequest request,HttpServletResponse reponse){
String msg ="add successfully";
request.setAttribute("msg", msg);
return "user/sucess";
}
9.测试msg页面很简单,一个el表达式搞定
<html>
<head>
</head>
<body>
msg=${msg }
</body>
</html>
关于详细的源码,源码下载地址(提取码:48f0),可以直接运行,多余的jar包大家酌情删除,由于完稿仓促,很多细节没有写好,源码我最近也重新修改了,我会尽快完善的。
有问题的同学可以加我企鹅:8078687