概述
本篇隨筆主要記錄內容如下:
1、通過Maven創建基於Spring Framework類庫的MVC項目,免去了繁瑣的XML配置;
2、在Idea里面配置Tomcat的測試啟動項;
Maven創建MVC項目
2.1、新建Maven項目:New Project-->Maven
2.2、修改配置
配置pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wisely</groupId> <artifactId>hightlight_springmvc4</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <java.version>1.7</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!--web--> <jsp.version>2.2</jsp.version> <jstl.version>1.2</jstl.version> <servlet.version>3.1.0</servlet.version> <!--spring--> <spring-framwork.version>4.1.5.RELEASE</spring-framwork.version> <!--Logging--> <logback.version>1.0.13</logback.version> <slf4j.version>1.7.5</slf4j.version> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> <!--Spring MVC--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-framwork.version}</version> </dependency> <!--WEB依賴--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp.version}</version> <scope>provided</scope> </dependency> <!--Spring and Transactions--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-framwork.version}</version> </dependency> <!--使用SLF4J和LogBack作為日志--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>${logback.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
在src/main/resources新建logback.xml來配置日志
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="1 seconds"> <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"> <resetJUL>true</resetJUL> </contextListener> <jmxConfigurator/> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>logbak: %d{HH:mm:ss.SSS} %logger{36} - %msg%n</pattern> </encoder> </appender> <logger name="org.springframework.web" level="DEBUG"/> <!-- 1 --> <root level="info"> <appender-ref ref="console"/> </root> </configuration>
在src/main/resources新建views文件夾,在src/main/resources/views新建index.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首頁</title> </head> <body> <pr> Welcome to Spring Mvc World </pr> </body> </html>
PS(右鍵views新建的時候並沒有jsp類型,需要將views設置為web路徑。Project Structure-->Modules-->選擇web,設置Web Resource Directory)
2.3、新建代碼塊
新建MvcConfig文件,用於配置MVC的映射規則,主要使用的InternalResourceViewResolver 類;PS:通過Package,查找新建的類的路徑
package com.wisely.highlight_springmvc4; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @Configuration @EnableWebMvc @ComponentScan("com.wisely.highlight_springmvc4") public class MyMVCConfig { @Bean public InternalResourceViewResolver viewResolver(){ InternalResourceViewResolver viewResolver=new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/classes/views/"); viewResolver.setSuffix(".jsp"); viewResolver.setViewClass(JstlView.class); return viewResolver; } }
新建WebInitializer繼承WebApplicationInitializer,重寫里面OnStartUp方法。在WEB中,我們使用AnnotationConfigWebApplicationContext,代碼如下
package com.wisely.highlight_springmvc4; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration.Dynamic; public class WebInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext context=new AnnotationConfigWebApplicationContext(); context.register(MyMVCConfig.class); context.setServletContext(servletContext); Dynamic servlet=servletContext.addServlet("dispatcher",new DispatcherServlet(context)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); } }
新建HelloController
package com.wisely.highlight_springmvc4.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/index") public String hello(){ return "index"; } }
Idea里面配置Tomcat
3.1、選擇Edit Configration,設置Tomcat
3.2、設置Deployment,選擇"+"之后,選擇Artifacts
PS:war模式—-將WEB工程以包的形式上傳到服務器 ;war exploded模式—-將WEB工程以當前文件夾的位置關系上傳到服務器
3.3、設置Server內容項
啟動項目RunTomcat即可,輸入 http://localhost:8009/H99/即可訪問頁面信息
get中文亂碼
項目中遇到在前台用get方法傳遞中文到后台拿到的是亂碼問題,花了好長時間解決,
下面是解決辦法:
1、在IDEA編輯器里的編碼格式都改成utf-8。
2、Get方式的亂碼問題,由於參數是通過URL傳遞的,需要在服務器端配置URL編碼格式。
注意,IDEA是沒法編輯server.xml文件的,需要我們手動編輯文件。
在D:\tolls\apache-tomcat-7.0.64\conf下,修改tomcat的配置文件server.xml:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
只需增加 URIEncoding="UTF-8" 這一句,然后重啟tomcat即可。
Post中文亂碼
package com.wisely.highlight_springmvc4; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.DispatcherServlet; import javax.servlet.*; import java.util.EnumSet; public class WebInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext context=new AnnotationConfigWebApplicationContext(); context.register(MyMVCConfig.class); context.setServletContext(servletContext); //Post中文編碼亂碼 CharacterEncodingFilter characterEncodingFilter=new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); characterEncodingFilter.setForceEncoding(true); FilterRegistration.Dynamic filter=servletContext.addFilter("characterEncodingFilter",characterEncodingFilter); filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); //映射Filter //數據啟動 ServletRegistration.Dynamic servlet=servletContext.addServlet("dispatcher",new DispatcherServlet(context)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); //開啟異步方法處理 servlet.setAsyncSupported(true); } }