日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

springMVC——Xml配置方式实现Helloworld

發布時間:2024/2/28 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springMVC——Xml配置方式实现Helloworld 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Xml配置helloworld

  • maven創建war項目
  • ?

  • 添加java和resources目錄,并修改類型
  • ?

    ?

    ?

  • 修改web.xml配置文件
  • <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    ?????????xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    ?????????version="3.1">

    </web-app>

  • 通過maven添加依賴
  • <?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.henu</groupId>
    ??<artifactId>springmvc_helloword_xml</artifactId>
    ??<version>1.0-SNAPSHOT</version>
    ??<packaging>war</packaging>

    ??<name>springmvc_helloword_xml Maven Webapp</name>
    ??<!-- FIXME change it to the project's website -->
    ??<url>http://www.example.com</url>

    ??<properties>
    ????<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    ????<maven.compiler.source>1.8</maven.compiler.source>
    ????<maven.compiler.target>1.8</maven.compiler.target>
    ??</properties>

    ??<dependencies>
    ????<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    ????<dependency>
    ??????<groupId>org.springframework</groupId>
    ??????<artifactId>spring-web</artifactId>
    ??????<version>4.3.20.RELEASE</version>
    ????</dependency>
    ????<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    ????<dependency>
    ??????<groupId>org.springframework</groupId>
    ??????<artifactId>spring-webmvc</artifactId>
    ??????<version>4.3.20.RELEASE</version>
    ????</dependency>
    ????<!-- 配置servlet -->
    ????<dependency>
    ??????<groupId>javax.servlet</groupId>
    ??????<artifactId>javax.servlet-api</artifactId>
    ??????<version>4.0.1</version>
    ??????<scope>provided</scope>
    ????</dependency>

    ????<!-- 配置jsp -->
    ????<dependency>
    ??????<groupId>javax.servlet.jsp</groupId>
    ??????<artifactId>javax.servlet.jsp-api</artifactId>
    ??????<version>2.3.3</version>
    ??????<scope>provided</scope>
    ????</dependency>

    ??</dependencies>

    </project>

  • 創建控制器
  • package com.henu.controller;


    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
    ?* @author George
    ?* @description
    ?**/

    public class UserController implements Controller {

    ????/**
    ?????* 處理器方法
    ?????*
    ?????* @param httpServletRequest
    ?????* @param httpServletResponse
    ?????* @return
    ?????* @throws Exception
    ?????*/
    ????@Override
    ????public ModelAndView handleRequest(HttpServletRequest httpServletRequest,
    ??????????HttpServletResponse httpServletResponse) throws Exception {

    ????????//創建模型視圖對象
    ????????ModelAndView mav = new ModelAndView();
    ????????//把數據綁定到模型對象
    ????????mav.addObject("name","admin");
    ????????//設置跳轉的試圖對象
    ????????mav.setViewName("success");
    ????????return mav;
    ????}
    }

    ?

  • 在web.xml中加載springMVC配置文件
  • <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    ?????????xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    ?????????version="3.1">
    ????<!--配置前端控制器-->
    ????<servlet>
    ????????<servlet-name>DispatcherServlet</servlet-name>
    ????????<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    ????????<!--加載自定義springmvc配置文件-->
    ????????<init-param>
    ????????????<param-name>contextConfigLocation</param-name>
    ????????????<param-value>classpath:springMVC.xml</param-value>
    ????????</init-param>
    ????????<!--配置自啟動-->
    ????????<!-- 配置springmvc什么時候啟動,參數必須為整數 -->
    ????????<!-- 如果為0或者大于0,則springMVC隨著容器啟動而啟動 -->
    ????????<!-- 如果小于0,則在第一次請求進來的時候啟動 -->
    ????????<load-on-startup>1</load-on-startup>
    ????</servlet>
    ????<servlet-mapping>
    ????????<servlet-name>DispatcherServlet</servlet-name>
    ????????<url-pattern>/</url-pattern>
    ????</servlet-mapping>

    </web-app>

    ?

    ?

    <!-- 配置springmvc什么時候啟動,參數必須為整數 -->
    ????????<!-- 如果為0或者大于0,則springMVC隨著容器啟動而啟動 -->
    ????????<!-- 如果小于0,則在第一次請求進來的時候啟動 -->
    ????????<load-on-startup>1</load-on-startup>

    <!--DispatcherServlet-servlet.xml-->
    <!-- 服務器啟動創建servlet對象 ,同時加載springmvc配置文件,默認是在WEB-INF下加載一個叫[servlet-name]-servlet.xml-->

    ?

  • 自定義springMVC配置文件
  • <?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:aop="http://www.springframework.org/schema/aop"
    ???????xmlns:context="http://www.springframework.org/schema/context"
    ???????xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    ????<!--配置控制器-->
    ????<bean name="/login" class="com.henu.controller.UserController"></bean>
    ????<!--映射處理器 -->
    ????<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    ????<!--處理器適配器-->
    ????<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    ????<!--視圖解析器-->
    ????<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    ????????<!--指定跳轉位置?/success.jsp-->
    ????????<property name="prefix" value="/"/>
    ????????<property name="suffix" value=".jsp"/>
    ????</bean>
    </beans>

  • idea配置tomcat
  • 創建success.jsp

  • <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    ????<title>Title</title>
    </head>
    <body>
    Hello World<br>
    ${name}<br>
    ${requestScope.name}<br>
    </body>
    </html>

  • 測試
  • 根據自己的配置,然后在瀏覽器中輸入路徑:

    http://localhost:9999/springmvc_helloword_xml_war_exploded/login

  • 使用springmvc自帶的過濾器解救post亂碼
  • <!-- POST中文亂碼過濾器 -->
    <filter>
    ????<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    ????<url-pattern>/*</url-pattern>
    </filter-mapping>

  • maven中使用tomcat插件運行web項目
  • pom.xml文件中配置tomcat插件:

    <build>
    ??<!-- 配置tomcat插件,web-->
    ??<plugins>
    ????<plugin>
    ??????<groupId>org.apache.tomcat.maven</groupId>
    ??????<artifactId>tomcat7-maven-plugin</artifactId>
    ??????<version>2.2</version>
    ??????<configuration>
    ????????<!-- 配置項目的請求路徑 -->
    ????????<path>/mvc</path>
    ????????<!-- 配置服務器端口號 -->
    ????????<port>9091</port>
    ??????</configuration>
    ????</plugin>
    ??</plugins>
    </build>

    ?

    超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

    總結

    以上是生活随笔為你收集整理的springMVC——Xml配置方式实现Helloworld的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。