springMVC学习总结(一) --springMVC搭建
搭建项目
1.创建一个web项目,并在项目中的src文件夹下创建一个包com.myl.controller.
2.添加相应jar包
3.在com.myl.controller包下创建一个Java类InderController
4.在/WEB-INF
文件夹下创建Spring配置文件web.xml
和springmvc-servler.xml
。
创建完成后,整个工程的目录结构如下图所示
IndexController.java
package com.myl.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; /** * * @author myl * @date 2018年5月19日 上午9:35:33 */ @Controller public class InderController { @RequestMapping("/index") public String index(ModelMap model) { model.addAttribute("message", "Hello Spring MVC"); return "index"; } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>springMVC Application</display-name> <servlet> <!-- 如果不配做<init-param><init-param>的话 配置文件是默认的 servlet-name + servlet.xml 例如我的servlet-name 是springmvc 默认配置文件就必须是springmvc-servlet.xml servlet-name名称必须和springmvc-servlet.xml对应 --> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.myl"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
index.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>Insert title here</title> </head> <body> <div> 我的第一个页面 jsp下面 ${message } </div> </body> </html>
需要jar包
- servlet-api-x.y.z.jar
- commons-logging-x.y.z.jar
- spring-aop-x.y.z.jar
- spring-beans-x.y.z.jar
- spring-context-x.y.z.jar
- spring-core-x.y.z.jar
- spring-expression-x.y.z.jar
- spring-webmvc-x.y.z.jar
- spring-web-x.y.z.jar
运行结果