标签:root prefix nal 图片 library display return html4 pre
一、准备工作
eclipse使用maven搭建项目,项目名称为HelloSpringMVC
二、搭建好的项目如下:
项目介绍:实现简单的helloworld
三、具体代码
controller类:HelloWorldController
package com.cn.springmvc; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorldController { @RequestMapping("/hello") //访问http://localhost:8080/HelloSpringMVC/hello public String hello(Model model){ model.addAttribute("greeting","hello Spring MVC"); //jsp页面接收 ${greeting} return "helloworld"; //跳转到 helloworld.jsp } }
jsp页面:helloworld.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Spring4 MVC -HelloWorld</title> </head> <body> <h1>${greeting}</h1> </body> </html>
web项目配置: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"> <display-name>HelloSpringMVC</display-name> <servlet> <!-- 加载springmvc配置: 1、这里的servlet-name默认指向“spring-mvc-servlet.xml” 2、手动指定:<init-param> --> <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:spring-mvc-servlet.xml</param-value> </init-param> --> <!-- <load-on-startup>:容器启动时,加载这个servlet的顺序 取值:0,1,2,3... 数字越小,越先加载 --> <load-on-startup>1</load-on-startup> </servlet> <!-- url-pattern:servlet访问地址url配置 /* path.endsWith("/*") *. path.starsWith("*.") / path.equals("/") 其他 --> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 加载其他的xml配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/root-context.xml</param-value> </context-param> <!-- Spring 监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
springMVC配置:spring-mvc-servlet.xml,root-context.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 自动扫描注解 --> <context:component-scan base-package="com.cn.springmvc"/> <!-- 处理静态资源,如img等,这类资源不通过DispatcherServlet转发,由容器自己处理 <mvc:default-servlet-handler default-servlet-name="所使用的Web服务器默认使用的Servlet名称" /> 对静态资源文件的访问 <mvc:resources mapping="/img/**" location="/img/" /> --> <mvc:default-servlet-handler /> <!-- <mvc:resources /> --> <!-- 使用注解时需要配置这个,springMVC为@Controller分发请求所必须的 --> <mvc:annotation-driven/> <!-- 视图控制器: 如果返回的是视图逻辑名称,如success,那么视图解析器就必须配置 如果返回的是真实视图名称,如/index.jsp,那么视图解析器为可选配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/pages/" /> <!-- 前缀 --> <property name="suffix" value=".jsp" /> <!-- 后缀 --> </bean> </beans>
pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cn</groupId> <artifactId>HelloSpringMVC</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>HelloSpringMVC Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Servlet Library --> <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- Spring dependencies --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> <build> <finalName>HelloSpringMVC</finalName> </build> </project>
四、访问:http://localhost:8080/HelloSpringMVC/hello
标签:root prefix nal 图片 library display return html4 pre
原文地址:https://www.cnblogs.com/x-jingxin/p/9641940.html