一、摘要
1、所需软件列表:
1) tomcat : apache-tomcat-7.0.54 服务端容器
2) Intellij: Intellij IDEA 14.0.3 开发工具
3) Syslog: SQLyog Community 数据库工具
2. 步骤简述:
1) 新建一个 Java 项目,在项目下新建一个文件夹 test(置于tomcat webapps 文件夹目录下) ,然后在该文件夹下新建一个 WEB-INF 文件夹;
2) test文件夹下新建前台页面相关目录(css/js/page/data等)
3) 在 WEB-INF文件夹下建立 web.xml 文件(可以从 tomcat 安装路径 /conf/web.xml 中拷贝基本的文档结构,修改相应编码为 utf-8);
4) WEB-INF文件夹下新建classes文件夹,用于存放相关配置文件
5) 在 WEB-INF 下建立 lib 文件夹,用来存放相关 jar 包;
6) 在 WEB-INF 下建立 web.xml 文件。
完成后的工程目录如下:
二、Spring配置:
1、需要导入的包如下:
2、 相关配置文件
1)web.xml配置
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </context-param> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class> </listener> </web-app >
2)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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <context:component-scan base-package="com.test"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <context:property-placeholder location="classpath:jdbc.properties"/> <mvc:annotation-driven></mvc:annotation-driven> <mvc:default-servlet-handler/> 注意这里不配置时静态页面会无法访问 <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page" /> <property name="suffix" value=".html" /> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
3) 对应的数据库配置文件 jdbc.properties 根据实际情况配置
jdbc.driverClassName= 数据库驱动 jdbc.url= 数据库连接url jdbc.username= 用户名 jdbc.password= 密码
三、 服务端代码
User实体类: public class UserEntity { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 数据库交互相关: public class UserDAO { private static final String querySql = "SELECT * FROM user;"; private static UserDAO instance = new UserDAO(); private UserDAO(){ } public static UserDAO getInstance(){ return instance; } public List<UserEntity> getUserInfo(){ //启动IoC容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-servlet.xml"); //获取IoC容器中JdbcTemplate实例 JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate"); RowMapper<UserEntity> rowMapper = new BeanPropertyRowMapper<UserEntity>(UserEntity.class); return jdbcTemplate.query(querySql, rowMapper); } } Controller入口: @Controller public class UserQuery { @RequestMapping("/getUserInfo") @ResponseBody public String getUserInfo() { List<UserEntity> userList = UserDAO.getInstance().getUserInfo(); return ParseUtil.parse(userList); } }
三、 Tomcat配置:
1)将整个test文件夹放入 tomcat 解压包的 apache-tomcat-7.0.54\webapps\ 目录下;
2) 将编译后的jar包放入 WEB-INF 目录下的lib文件夹下
ps: tomcat 的debug方法:
tomcat bin文件夹下修改 catalina.bat 文件,增加如下配置:
SET CATALINA_OPTS=-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8899
四、 效果展示:
1. 数据库原有数据:
2. 界面查询:
3. 静态界面展示: