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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringMVC搭建+实例

發(fā)布時間:2023/12/1 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringMVC搭建+实例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

想做一點自己喜歡的東西,研究了一下springMVC,所以就自己搭建一個小demo,可供大家吐槽。

  • 先建一個WEB工程,這個相信大家都會,這里不在多說。
  • 去網(wǎng)上下載spring jar包,然后在WEB-INF下新建一個lib文件,將下載的jar包放進去。如下圖


  • 在WEB/INF下建一個web.xml文件,內(nèi)容如下:
    <?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_3_0.xsd"id="WebApp_ID" version="3.0"><display-name>springMVCtest</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>springMVCtest</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMVCtest</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 加載spring的xml配置文件到 spring的上下文容器中 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param> </web-app>

    注意:servlet-name的名字必須和接下來要創(chuàng)建的servlet.xml的名字要對應(yīng)。 context-param指定applicationContext.xml 文件的位置。

  • 在WEB-INF下新建一個xml,因為我上文中定義的名字是springMVCtest,所以,文件名字為springMVCtest-servlet.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:task="http://www.springframework.org/schema/task"xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd "><!-- 使用默認的注解映射 <mvc:annotation-driven /><mvc:resources location="/" mapping="/index.html" /><context:annotation-config></context:annotation-config>--><!-- 自動掃描controller包中的控制器 --><context:component-scan base-package="com.controller" /><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><props><prop key="/index.do">controllerDoem</prop></props></property></bean><bean id="controllerDoem" class="com.controller"><property name="view"><value>index</value></property></bean>--></beans>
  • 在src目錄下新建文件applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"> </beans>
  • 在src下新建一個包名,com.controller,新建一個類DemoController,注:這里的類名必須已Controller結(jié)尾,之前我寫其他的類名,然后項目一直404,后來改成這樣之后就好了,具體原因,還沒搞清楚,知道的大神歡迎回復(fù)。
    package com.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;@Controller public class DemoController {@RequestMapping("/index")public ModelAndView index(){//創(chuàng)建模型跟視圖,用于渲染頁面。并且指定要返回的頁面為index頁面ModelAndView mav = new ModelAndView("index");return mav;}}
  • 最后一步,在WEB-INF下新建目錄jsp,里面新建文件index.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body><h1>hahahaha</h1> </body> </html>
  • 將項目部署到tomcat下,啟動tomcat,就可以運行了,訪問http://localhost:8080/springMVCtest/index

    最后配上整個項目結(jié)構(gòu):

    搞定!(如有錯誤,請大神指出。)
  • 轉(zhuǎn)載于:https://www.cnblogs.com/mei0619/p/6560332.html

    總結(jié)

    以上是生活随笔為你收集整理的SpringMVC搭建+实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。