struts+spring+mybatis介绍
Java Resources
action 处理请求返回结果
entity 实体对象
mybatis 简单ORM映射
service 业务处理
interceptor 拦截器
globalMessages.properties 全局资源文件
log4j.properties log4j日志
struts.xml struts配置文件
mybatis.xml mybatis配置文件
web/index.jsp jsp页面文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello</title> </head> <body> <h2>Hello!</h2> </body> </html>
web/WEB-INF/applicationContext.xml 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- Begin Base --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.1.199:3307/gwanda?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <!-- sqlSessionFactory工厂bean,如果有定义别名等mybatis基本配置 需要通过configLocation属性指定mybatis配置文件所在位置 如果mapper.xml和接口interface不在同一个目录,需要通过mapperLocations属性指定所有mapper.xml文件的位置,如: <property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" /> 因为本项目的mapper.xml和interface在同一目录,故省略此属性的配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis.xml"></property> </bean> <!-- MapperScannerConfigurer会自动创建所有MapperFactoryBean,通过以下配置可以不用再手工把所 的MapperFactoryBean加入到spring的文件中,MapperFactoryBean是指以下的配置: <bean id="schoolMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value=" cn.e21.lesson.mybatis.mappers.SchoolMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> 通过配置MapperScannerConfigurer,可以省略MapperFactoryBean在spring中的配置 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="mappers映射文件夹" /> </bean> <!-- End Base --> <!-- Begin DAO --> <!-- End DAO --> <!-- Begin Service --> <!-- End Service --> <!-- Begin Struts2 Action --> <!-- End Struts2 Action --> </beans>
web/WEB-INF/web.xml 全局配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>gwanda</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <!-- *.action --> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> </web-app>

浙公网安备 33010602011771号