SpringMVC程序簡單實(shí)例
StringMVC程序簡單實(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"> <!-- 注冊aop功能 --> <aop:aspectj-autoproxy/> <!-- 自動掃面com.springmvc目錄及其子目錄下面所有類文件,自動注入所有帶注解的類 --> <context:component-scan base-package="com.yxl.*" /> <!-- 處理請求response返回值,如下配置能正確返回字符串型返回值,如返回值為對象,則自動轉(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" /> <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前后綴 --> <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)建一個控制器。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Spring mvc實(shí)現(xiàn)Restful返回json格式數(shù)據(jù)實(shí)例詳解
這篇文章主要介紹了Spring mvc實(shí)現(xiàn)Restful返回json格式數(shù)據(jù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
jsp中獲取狀態(tài)怎么寫(兩種實(shí)現(xiàn)方式)
由于框架是2005年的,jsp中不能存放標(biāo)簽,只能有java代碼來寫了,接下來為大家介紹下兩種實(shí)現(xiàn)獲取狀態(tài)寫法,感興趣的各位可以參考下哈,希望可以幫助到你2013-04-04
基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
實(shí)例講解JSP獲取ResultSet結(jié)果集中的數(shù)據(jù)的方法
這篇文章主要介紹了JSP獲取ResultSet結(jié)果集中數(shù)據(jù)的方法,文后還介紹一種遍歷ResultSet中的數(shù)據(jù)并轉(zhuǎn)化為表格的方法,需要的朋友可以參考下2016-04-04
基于JSP的動態(tài)網(wǎng)站開發(fā)技術(shù)
基于JSP的動態(tài)網(wǎng)站開發(fā)技術(shù)...2006-10-10

