参考URL:
https://www.cnblogs.com/lnsylt/p/10258457.html
■目録
■環境設定
①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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>cn.com.sy</groupId>
<artifactId>testspringmvc</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version> <!-- エンコード(UTF8)を指定 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.2.RELEASE</spring.version>
</properties> <dependencies> <!-- SpringMVCのJARを指定 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency> <!-- WebファイルのJAR -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency> <!-- ApacheのCommonsのJAR -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>commons-chain</groupId>
<artifactId>commons-chain</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-digester3</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency> <!-- ログ出力のJAR -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency> <!-- DataBase -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency> <!-- json jar -->
<!-- <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-all</artifactId> <version>1.8.5</version> </dependency> --> </dependencies>
</project>
pom.xml
②web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
http://www.springmodules.org/schema/cache/springmodules-cache.xsd
http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Archetype Created Web Application</display-name> <!--/WEB-INF/applicationContext.xml を導入(BeanのContainer) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param> <!-- リスナーの設定 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 文字化けの設定 -->
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- DispatcherServletを指定する -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 拡張XMLを指定する「;」で区切り -->
<param-value>classpath*:/springmvc.xml</param-value>
</init-param>
<!-- <load-on-startup>1</load-on-startup> -->
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 請求URLに「*.do」を指定する、「"/"」を指定すると、すべての内容をインターセプトする(intercept) -->
<url-pattern>*.do</url-pattern>
</servlet-mapping> </web-app>
web.xml
注意:<web-app/>はVer.2.5以上を利用する必要
③「applicationContext.xml」はSpringの専用の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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- Beanのパス(サブパスを含む)を検索 -->
<context:component-scan base-package="com.springdemo.service" /> <!-- DB配置ファイル -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- データベースの設定 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" /> <context:annotation-config /> <!-- jdbcTemplateを導入 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource" /> <!-- 事務管理設定 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 事務の設定(@Tranctional) -->
<tx:annotation-driven transaction-manager="txManager" /> </beans>
applicationContext.xml
④「springmvc.xml」はSpringMVCの専用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:tx="http://www.springframework.org/schema/tx" 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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- コントローラのパス(サブパスを含む)を検索(@Controller、@Service) -->
<context:component-scan base-package="com.springdemo.*">
<!-- 「@Service」を除外 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan> <!-- HandlerMapper、HandlerAdapterを設定する(controller to controller) -->
<mvc:annotation-driven /> <!-- 静的なファイルを導入(方法一) -->
<mvc:default-servlet-handler /> <!-- 静的なファイルを導入(方法二) -->
<!-- <mvc:resources /> --> <!-- JSON対応 開始 ([<mvc:annotation-driven />]と競合)-->
<!--
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
<ref bean="utf8StringHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="utf8StringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
-->
<!-- JSON対応 終了 --> <!-- 応対ファイルの形を設定、Root目録は「”/”」、拡張子は「.jsp」です。」 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>
springmvc.xml
⑤log4j.properties
#---------------------------------------------------------------------------------------
# Logger
#---------------------------------------------------------------------------------------
# Root
log4j.rootLogger=INFO,STDOUT, ROOT #---------------------------------------------------------------------------------------
# Appender
#---------------------------------------------------------------------------------------
# for Root
log4j.appender.ROOT=org.apache.log4j.ConsoleAppender
log4j.appender.ROOT.Target=System.out
log4j.appender.ROOT.layout=org.apache.log4j.PatternLayout
log4j.appender.ROOT.layout.ConversionPattern=%-5p - %m%n # for SQL
log4j.appender.STDOUT=org.apache.log4j.RollingFileAppender
log4j.appender.STDOUT.File=C:/Users/LNSYLT/Tools/pleiades2018/log/STDOUT.log
log4j.appender.STDOUT.Append=true
log4j.appender.STDOUT.MaxFileSize=1MB
log4j.appender.STDOUT.MaxBackupIndex=20
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%d %-5.12p [%C{2}][%F][%M(%L)] - %m%n
log4j.properties
⑥jdbc.properties
#SET DB CONNECT
jdbc.driverClassName=""
jdbc.url=""
jdbc.username=""
jdbc.password=""
⑦DemoController.java
package com.springdemo.controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes; /**
* クラス名(任意) <br/>
* 請求パス(”/demo”)を指定すれば、 配下関数の請求パス("/demo/xxxx.do")、「.do」はXMLにセットする
*
* @author LNSYLT
*/
@Controller @RequestMapping(value = "/demo")
public class DemoController { public Logger log = Logger.getLogger(this.getClass()); /**
* 関数名(任意)<br/>
* クラスに請求パスを指定する場合、関数の請求方法は「"/demo/***.do"」です。
* クラスに請求パスを指定しない場合、関数の請求方法は「"***.do"」です。(唯一制御)
*
* @param req (任意)
* @param res (任意)
* @return
* @throws Exception
*/
@RequestMapping(value = "/login")
public String index(HttpServletRequest req, HttpServletResponse res) throws Exception {
log.info("index");
//web.xmlにて、文字化けを対応すれば、下記の方法一、二は不要となる
// 画面エンコードを設定(方法一)
//res.setCharacterEncoding("UTF-8");
// 画面エンコードを設定(方法二)
// res.setContentType("text/html;charset=UTF-8"); String key = (String) req.getParameter("KEY");
if (key != null && key.equals("val_index2forAttr")) {
log.info("このKEYは関数index2のAttrから戻る");
log.debug("このKEYは関数index2のAttrから戻る");
} // 応答ファイル:”/jsp/login.jsp”、ファイルの拡張子(.jsp)はXMLにセットする
return "/jsp/login";
} @RequestMapping(value = "/login2")
public String index2(HttpServletRequest req, RedirectAttributes attr) {
log.info("index2");
// 引き渡すパラメータを設定、このようにすれば、請求URLに「?KEY=XXX」で保持する
attr.addAttribute("KEY", "val_index2forAttr"); // 再度請求URL、キー「"redirect:"」
return "redirect:/demo/login.do";
}
}
DemoController.java
注意:Controller⇒Controllerの方法は「return "redirect:/demo/login.do"」にする。
⑧index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<h2>これは、初期化画面です。(http://localhost:8080/${pageContext.request.contextPath}/index.jsp)</h2>
</div>
</body>
</html>
index.jsp
⑨login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<h2><%="これは、サーバ側に通信した結果です。(http://localhost:8080/${pageContext.request.contextPath}/demo/login.do)" %></h2>
${KEY}
</div>
</body>
</html>
login.jsp