java的mvc框架,springMVC配置

 2023-10-18 阅读 24 评论 0

摘要:java的mvc框架?創建后如果index.jsp報錯重新新建就好。 添加jstl servlet springmvc相關配置 pom.xml <!-- servlet --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</ver

java的mvc框架?創建后如果index.jsp報錯重新新建就好。

添加jstl servlet springmvc相關配置

pom.xml

復制代碼
<!-- servlet --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><!-- end servlet --><!-- spring mvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.3.12.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.12.RELEASE</version></dependency><!-- end spring mvc --><!-- jstl --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!--end jstl -->
復制代碼

web.xml

?

復制代碼
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- spring 攔截器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- end spring 攔截器 --><!-- 加載 applicationContext.xml --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- 編碼過濾器 --><filter><filter-name>encoding</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>encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- end編碼過濾器 --><!-- 設置監聽 --><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:dispatcher-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- end設置監聽 --><!-- 默認頁面 --><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>
復制代碼

Spring mvc,創建兩個xml ? ? ? ? applicationContext.xml用于配置數據庫、事物等,dispatcher-servlet.xml配置試圖層相關

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:mvc="http://www.springframework.org/schema/mvc" 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/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd"></beans>
復制代碼

dispatcher-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:mvc="http://www.springframework.org/schema/mvc" 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/mvc  http://www.springframework.org/schema/mvc/spring-mvc.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd"><!-- 定義controller掃描包 --><context:component-scan base-package="com.example.controller" /><!-- 啟動注釋模式 --><mvc:annotation-driven /><!-- 處理靜態資源 --><mvc:default-servlet-handler /><!-- 設置不過濾內容 --><mvc:resources location="/*.html" mapping="/**.html" /><mvc:resources location="/css/*" mapping="/css/**" /><mvc:resources location="/js/*" mapping="/js/**" /><mvc:resources location="/images/*" mapping="/images/**" /><!-- 配置視圖解析器 --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean>
</beans>

轉載于:https://www.cnblogs.com/hufeng1989/p/7778527.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/146335.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息