基於tomcat+springMVC搭建基本的前后台交互系統


一、摘要

  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"/>

<bean id="fastJsonHttpMessageConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json</value>
</list>
</property>
<property name="features">
<list>
<!-- 輸出key時是否使用雙引號 -->
<value>QuoteFieldNames</value>
<!-- 是否輸出值為null的字段 -->
<value>WriteMapNullValue</value>
<!-- 數值字段如果為null,輸出為0,而非null -->
<value>WriteNullNumberAsZero</value>
<!-- List字段如果為null,輸出為[],而非null -->
<value>WriteNullListAsEmpty</value>
<!-- 字符類型字段如果為null,輸出為"",而非null -->
<value>WriteNullStringAsEmpty</value>
<!-- Boolean字段如果為null,輸出為false,而非null -->
<value>WriteNullBooleanAsFalse</value>
<!-- null String不輸出 -->
<value>WriteNullStringAsEmpty</value>
<!-- Date的日期轉換器 -->
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
<!-- 啟動注解,注冊服務,如驗證框架、全局類型轉換器-->
<mvc:annotation-driven>
<mvc:message-converters>
<ref bean="fastJsonHttpMessageConverter" />
</mvc:message-converters>
</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 UserBean{
    private int id;
    private String name;
    private int age;

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}


數據庫交互相關: 
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<UserBean> getUserInfo(){
        //啟動IoC容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-servlet.xml");
        //獲取IoC容器中JdbcTemplate實例
        JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");

        RowMapper<UserBean> rowMapper = new BeanPropertyRowMapper<UserBean>(UserBean.class);
        return jdbcTemplate.query(querySql, rowMapper);
    }
}


返回結果處理類(alibaba.fastjson格式返回)
public class ResultFormat {
    private Object object;
    public ResultFormat() {}

    public ResultFormat(Object object) {
        this.object = object;
    }

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }
}


Controller實現類:
@Controller
public class UserController {
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public ResultFormat getUserInfo() {
        List<UserBean> userList = UserDAO.getInstance().getUserInfo();
        return new ResultFormat(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. 靜態界面展示:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM