1. 程式人生 > >springMVC之與jsp頁面互動1

springMVC之與jsp頁面互動1

突然想寫這個的目的就是網上寫的教程大多都不全,不夠系統,想借此來進行系統的學習,讓初學者不在迷茫,此外也可以當做自己的移動筆記,讓自己對這些框架更加熟悉,如有疑問歡迎大家來交流。

接下來要寫的主要是springMVC與jsp的互動,springMVC與安卓的互動,mybatis與資料庫的互動,springMVC與mybatis整合與jsp的互動,與安卓的互動,在ssm中加入Redis,主要寫這幾大塊,在接下來的時間完成,要做的事比較多,可能花的時間也比較長,我儘量寫的詳細點,讓大家都能懂,都能看著部落格把基本的程式跑通了。

框架的搭建

這篇部落格將主要描述一下框架的搭建,搭建springMVC,mybits暫不涉及,如果自己已會請直接跳到下篇。
本人的環境為eclipse(Eclipse Java EE IDE for Web Developers. Version: Oxygen.1 Release (4.7.1)
Build id: 20170914-1200)+jdk1.8+tomcat7.0 。這些網上都有現成的,請自行百度,請相信你所有將遇到的問題都有人遇到過。

1

首先,開啟eclipse file–>new–>Dynamic Web Project。由於圖片不知怎麼了貼不上來,文字代替,後面貼上。
這裡寫圖片描述
找不到Dynamic Web Project就到other裡面去搜。

然後在WebContent–>WEB-INF–>lib匯入spring的jar包,在後面會貼出來,自己想了解也可以去官網瞭解
這裡寫圖片描述

2

再WEB-INF下建立一個jsp的資料夾,建立一個jsp檔案test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>標題</title>
</head>
<body>
2333
</body>
</html>  

src下右鍵建立一個包com.spring.test1,在包下建一個class SpringMVCTest1.java,新增相應的註解帶@都是註解

package com.springmvc.test1;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/tsetClass")
public class SpringMVCTest1 {

    @RequestMapping("/testMethod")
    public ModelAndView test() throws Exception{
        ModelAndView mv = new ModelAndView();
        mv.setViewName("test"); 
        System.out.println("hello springMVC");
        return mv;

    }

}

3

在java Resource資料夾下建立source folder資料夾config,建立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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo 
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- 配置@Controller註解掃描,即定義裝載掃描的包 -->
<context:component-scan base-package="com.springmvc.test1"></context:component-scan>

<!-- 如果沒有顯示的配置處理器對映器和處理器適配那麼springMvc會去預設的dispatcherServlet.properties中查詢,
對應的處理器對映器和處理器介面卡去使用,這樣每個請求都要掃描一次他的預設配置檔案,效率非常低,會降低訪問速度,所以要顯示的配置處理器對映器和
處理器介面卡 -->

<!-- 註解形式的處理器對映器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
<!-- 註解形式的處理器介面卡 -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> -->

<!-- 配置最新版的註解的處理器對映器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
<!-- 配置最新版的註解的處理器介面卡 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->

    <!-- 註解驅動:
        作用:替我們自動配置最新版的註解的處理器對映器和處理器介面卡
     -->
    <mvc:annotation-driven></mvc:annotation-driven>


    <!-- 配置檢視解析器 
    作用:在controller中指定頁面路徑的時候就不用寫頁面的完整路徑名稱了,可以直接寫頁面去掉副檔名的名稱
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 真正的頁面路徑 =  字首 + 去掉字尾名的頁面名稱 + 字尾 -->
        <!-- 字首 -->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!-- 字尾 -->
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>  

## 4 ##
然後在WebContent–>WEB-INF目錄下web.xml檔案中加入

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVC1024</display-name>
  <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!-- 如果沒有指定springMvc核心配置檔案那麼預設會去找/WEB-INF/+<servlet-name>中的內容 +   -servlet.xml配置檔案 -->
    <!-- 指定springMvc核心配置檔案位置 -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:SpringMvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>