zoukankan      html  css  js  c++  java
  • 【Spring】SpringMVCの環境構築(簡)(Version3.1)

    ■Mavenでプロジェクトの新規

    ■プロジェクトのイメージ

    ■必要なラブリア

    ■ソース

    ①pom.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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">
     3 
     4     <modelVersion>4.0.0</modelVersion>
     5     <groupId>cn.com.sy</groupId>
     6     <artifactId>testspringmvc</artifactId>
     7     <packaging>war</packaging>
     8     <version>0.0.1-SNAPSHOT</version>
     9 
    10     <!-- エンコード(UTF8)を指定 -->
    11     <properties>
    12         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    13         <spring.version>3.1.2.RELEASE</spring.version>
    14     </properties>
    15 
    16     <dependencies>
    17 
    18         <!-- SpringMVCのJARを指定 -->
    19         <dependency>
    20             <groupId>org.springframework</groupId>
    21             <artifactId>spring-webmvc</artifactId>
    22             <version>${spring.version}</version>
    23         </dependency>
    24 
    25         <!-- WebのJARを指定 -->
    26         <dependency>
    27             <groupId>javax.servlet</groupId>
    28             <artifactId>javax.servlet-api</artifactId>
    29             <version>3.0.1</version>
    30             <scope>provided</scope>
    31         </dependency>
    32         <dependency>
    33             <groupId>javax.servlet</groupId>
    34             <artifactId>jstl</artifactId>
    35             <version>1.1.2</version>
    36             <scope>provided</scope>
    37         </dependency>
    38         <dependency>
    39             <groupId>javax.servlet.jsp</groupId>
    40             <artifactId>javax.servlet.jsp-api</artifactId>
    41             <version>2.3.1</version>
    42             <scope>provided</scope>
    43         </dependency>
    44 
    45         <!-- json jar -->
    46         <!-- <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-all</artifactId> <version>1.8.5</version> </dependency> -->
    47 
    48     </dependencies>
    49 </project>
    pom.xml

    ②web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE web-app PUBLIC
     3  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     4  "http://java.sun.com/dtd/web-app_2_3.dtd" >
     5 
     6 <web-app>
     7     <display-name>Archetype Created Web Application</display-name>
     8 
     9     <!-- DispatcherServletを指定する -->
    10     <servlet>
    11         <servlet-name>springmvc</servlet-name>
    12         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    13         <init-param>
    14             <param-name>contextConfigLocation</param-name>
    15             <!-- 拡張XMLを指定する「;」で区切り -->
    16             <param-value>classpath:springmvc-servlet.xml</param-value>
    17         </init-param>
    18         <!-- <load-on-startup>1</load-on-startup> -->
    19     </servlet>
    20 
    21     <servlet-mapping>
    22         <servlet-name>springmvc</servlet-name>
    23         <!-- 請求URLに「*.do」を指定する -->
    24         <url-pattern>*.do</url-pattern>
    25     </servlet-mapping>
    26 
    27 </web-app>
    web.xml

    ③springmvc-servlet.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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 
     3        http://www.springframework.org/schema/beans/spring-beans.xsd 
     4        http://www.springframework.org/schema/context 
     5        http://www.springframework.org/schema/context/spring-context.xsd 
     6        http://www.springframework.org/schema/tx 
     7        http://www.springframework.org/schema/tx/spring-tx.xsd
     8           http://www.springframework.org/schema/mvc
     9        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    10 
    11     <!-- コントローラのパス(サブパスを含む)を検索 -->
    12     <context:component-scan base-package="com.springdemo.*" />
    13     
    14     <!-- HandlerMapper、HandlerAdapterを設定する(controller to controller) -->
    15     <mvc:annotation-driven />
    16 
    17     <!-- 静的なファイルを導入 -->
    18     <mvc:default-servlet-handler />
    19     
    20     <!-- 応対ファイルの形を設定、Root目録は「”/”」、拡張子は「.jsp」です。」 -->
    21     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    22         <property name="prefix" value="/"></property>
    23         <property name="suffix" value=".jsp"></property>
    24     </bean>
    25 
    26 </beans>
    springmvc-servlet.xml

    ④index.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
     2 <!DOCTYPE html>
     3 <html>
     4 <head>
     5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     6 <title>Insert title here</title>
     7 </head>
     8 <body>
     9     <div>
    10         <h2>これは、初期化画面です。(http://localhost:8080/testspringmvc/index.jsp)</h2>
    11     </div>
    12 </body>
    13 </html>
    index.jsp

    ⑤login.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
     2 
     3 <!DOCTYPE html>
     4 <html>
     5 <head>
     6 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <div>
    11         <h2><%="これは、サーバ側に通信した結果です。(http://localhost:8080/testspringmvc/demo/login.do)" %></h2>
    12     </div>
    13 </body>
    14 </html>
    login.jsp

    ⑥DemoController.java

     1 package com.springdemo.controller;
     2 
     3 import javax.servlet.http.HttpServletRequest;
     4 import javax.servlet.http.HttpServletResponse;
     5 
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.servlet.mvc.support.RedirectAttributes;
     9 
    10 /**
    11  * クラス名(任意) <br/>
    12  * 請求パス(”/demo”)を指定すれば、 配下関数の請求パス("/demo/xxxx.do")、「.do」はXMLにセットする
    13  * 
    14  * @author LNSYLT
    15  */
    16 @Controller
    17 @RequestMapping(value = "/demo")
    18 public class DemoController {
    19 
    20     /**
    21      * 関数名(任意)<br/>
    22      * クラスに請求パスを指定する場合、関数の請求方法は「"/demo/***.do"」です。
    23      * クラスに請求パスを指定しない場合、関数の請求方法は「"***.do"」です。(唯一制御)
    24      * 
    25      * @param req (任意)
    26      * @param res (任意)
    27      * @return
    28      * @throws Exception
    29      */
    30     @RequestMapping(value = "/login")
    31     public String index(HttpServletRequest req, HttpServletResponse res) throws Exception {
    32         System.out.println("index");
    33         // 画面エンコードを設定(方法一)
    34         res.setCharacterEncoding("UTF-8");
    35         // 画面エンコードを設定(方法二)
    36         // res.setContentType("text/html;charset=UTF-8");
    37 
    38         String key = (String) req.getParameter("KEY");
    39         if (key != null && key.equals("val_index2forAttr")) {
    40             System.out.println("このKEYは関数index2のAttrから戻る");
    41         }
    42 
    43         // 応答ファイル:”/jsp/login.jsp”、ファイルの拡張子(.jsp)はXMLにセットする
    44         return "/jsp/login";
    45     }
    46 
    47     @RequestMapping(value = "/login2")
    48     public String index2(HttpServletRequest req, RedirectAttributes attr) {
    49         System.out.println("index2");
    50         // 引き渡すパラメータを設定、このようにすれば、請求URLに「?KEY=XXX」で保持する
    51         attr.addAttribute("KEY", "val_index2forAttr");
    52 
    53         // 再度請求URL、キー「"redirect:"」
    54         return "redirect:/demo/login.do";
    55     }
    56 }
    DemoController.java
  • 相关阅读:
    在客户端模拟调用srv和topic
    直流电机测试标准
    vue项目修改host实现地址代理,实现一键登录
    小程序 日期格式化
    ES6学习笔记之async函数
    ES6学习笔记之promise
    ES6学习笔记之箭头函数
    ES6学习笔记之var,let,const
    axios post后台接收不到参数
    vue-cli2配置scss遇到的各种坑
  • 原文地址:https://www.cnblogs.com/lnsylt/p/10258457.html
Copyright © 2011-2022 走看看