專案目錄

一.創建maven專案,添加web支持,在pom.xml問價引入一下代碼
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
二..web模塊部分的代碼與上一小節的相同,新建jsp檔案,配置web.xml
1.web.xml代碼
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:annoSpring-mvc.xml</param-value> </context-param> <servlet> <servlet-name>Spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:annoSpring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2.jsp代碼
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
三.在Java檔案夾下創建com.xiaoma.controller.HelloControler類檔案,代碼如下
package com.xiaoma.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** * Author:xiaoma * FileName:HelloController * Data:2022/4/27 9:35 */ @Controller public class HelloController { @RequestMapping("hello") public String sayHello(Model model){ model.addAttribute("msg","hello"); //web-inf/jsp/hello.jsp return "hello"; } }
四.在resources檔案夾下創建annoSpring-mvc.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 自動掃描包,讓指定包下的注解生效,由IOC容器統一管理 --> <context:component-scan base-package="com.xiaoma.controller"/> <!--下面的代碼固定死的--> <!-- 讓Spring MVC不處理靜態資源 --> <mvc:default-servlet-handler /> <!-- 支持mvc注解驅動 在spring中一般采用@RequestMapping注解來完成映射關系 要想使@RequestMapping注解生效 必須向背景關系中注冊DefaultAnnotationHandlerMapping 和一個AnnotationMethodHandlerAdapter實體 這兩個實體分別在類級別和方法級別處理, 而annotation-driven配置幫助我們自動完成上述兩個實體的注入, --> <mvc:annotation-driven /> <!-- 視圖決議器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前綴 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后綴 --> <property name="suffix" value=".jsp" /> </bean> </beans>
五.配置并啟動tomcat,瀏覽器訪問localhost:8080/hello,結果界面上顯示“hello”字樣
注意1:tomcat的Deployment的Application context路徑為/
注意2:記得點擊idea選單欄的File-》Project Structure-》在Output Layout中添加lib檔案夾并添加所有依賴,否則會報404或者500的錯誤

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465943.html
標籤:Java
