佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > SpringMVC程序簡(jiǎn)單實(shí)例

SpringMVC程序簡(jiǎn)單實(shí)例

熱門標(biāo)簽:服務(wù)器配置 Linux服務(wù)器 電子圍欄 銀行業(yè)務(wù) 團(tuán)購(gòu)網(wǎng)站 Mysql連接數(shù)設(shè)置 科大訊飛語音識(shí)別系統(tǒng) 阿里云

StringMVC程序簡(jiǎn)單實(shí)例

第一步:導(dǎo)入jar包


第二步,在WEB-INF文件夾下創(chuàng)建spring-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:aop="http://www.springframework.org/schema/aop" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
 !-- 注冊(cè)aop功能 --> 
 aop:aspectj-autoproxy/> 
  
 !-- 自動(dòng)掃面com.springmvc目錄及其子目錄下面所有類文件,自動(dòng)注入所有帶注解的類 --> 
 context:component-scan base-package="com.yxl.*" /> 
  
 
 !-- 處理請(qǐng)求response返回值,如下配置能正確返回字符串型返回值,如返回值為對(duì)象,則自動(dòng)轉(zhuǎn)為json --> 
 bean id="handleAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
  property name="messageConverters"> 
   list> 
   ref bean="mappingJacksonHttpMessageConverter" />!-- json轉(zhuǎn)換器 --> 
   ref bean="mappingStringHttpMessageConverter" /> 
   /list> 
  /property> 
 /bean> 
 bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 
 bean id="mappingStringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter" /> 
 
 !-- 對(duì)模型視圖名稱的解析,即在模型視圖名稱添加前后綴 --> 
 bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
   property name="prefix" value="/WEB-INF/jsp/" /> 
   property name="suffix" value=".jsp">/property> 
   property name="order" value="1">/property> 
 /bean> 
 
 !-- spring文件上傳編碼 --> 
 bean id="multipartResolver" 
  class="org.springframework.web.multipart.commons.CommonsMultipartResolver" 
  p:defaultEncoding="utf-8" /> 
/beans> 
 
 

 第三步:在web.xml文件配置springmvc。

?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 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>springmvc1/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> 
 
 !-- 設(shè)置編碼 --> 
 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> 
  init-param> 
   param-name>forceEncoding/param-name> 
   param-value>true/param-value> 
  /init-param> 
 /filter> 
 
 
 !-- 配置DispatchcerServlet --> 
 servlet> 
  servlet-name>Spring MVC Dispatcher Servlet/servlet-name> 
  servlet-class>org.springframework.web.servlet.DispatcherServlet/servlet-class> 
  init-param> 
    !-- 配置Spring mvc下的配置文件的位置和名稱 --> 
   param-name>contextConfigLocation/param-name> 
   param-value> 
    /WEB-INF/spring-servlet.xml 
   /param-value> 
  /init-param> 
  load-on-startup>1/load-on-startup> 
 /servlet> 
 !-- 配置DispatchcerServlet 攔擊路徑'/'所有訪問都攔截--> 
 servlet-mapping> 
  servlet-name>Spring MVC Dispatcher Servlet/servlet-name> 
  url-pattern>//url-pattern> 
 /servlet-mapping> 
  
/web-app> 
 

 第四步:創(chuàng)建一個(gè)控制器。


 

 感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

您可能感興趣的文章:
  • springMVC使用jsp:include嵌入頁(yè)面的兩種方法(推薦)
  • JSP spring boot / cloud 使用filter防止XSS
  • JSP 獲取spring容器中bean的兩種方法總結(jié)
  • JSP Struts過濾xss攻擊的解決辦法
  • 利用JSP session對(duì)象保持住登錄狀態(tài)

標(biāo)簽:衡水 廣元 大理 棗莊 衢州 江蘇 萍鄉(xiāng) 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SpringMVC程序簡(jiǎn)單實(shí)例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    卢湾区| 阳江市| 丽水市| 长丰县| 财经| 临沧市| 南宁市| 齐齐哈尔市| 壶关县| 新余市| 康马县| 满洲里市| 天全县| 汾西县| 永新县| 佛学| 松桃| 绥化市| 油尖旺区| 濉溪县| 西和县| 花莲市| 怀柔区| 朝阳县| 突泉县| 印江| 宜州市| 乐山市| 博兴县| 高阳县| 武功县| 固始县| 综艺| 遂平县| 咸阳市| 衡阳县| 博客| 祁门县| 四川省| 咸宁市| 荣成市|