SpringMVC(一)——搭建环境
2021-05-11 02:27
标签:eclips lang location dex mod 运行 方法调用 核心 startup SpringMVC是Spring的一个模块。MVC=Model+View+Controller,用于与前端交互,处理前端请求。 1.eclipse创建项目 File->New->Dynamic Web Project->输入名称->next->next->勾选Generate web.xml deployment descriptor(默认生成一个web.xml)->finish 2.导包 在WebContent->WEB-INF->lib下 commons-logging spring-beans spring-context spring-core spring-expression(Spring的5个核心包) spring-web spring-webmvc(MVC的核心包) 3.在WebContent->WEB-INF->web.xml中配置SpringMVC的核心(前端)控制器DispatcherServlet 注意点,两个 4.创建控制器类,需要实现Controller接口,后面学会注解就用注解 5.配置springmvc-config.xml,配置控制器映射文件 6.创建视图页面 WebContent->WEB-INF->jsp->index.jsp 7.运行进入http://localhost:8080/MVC_01/firstController 由于我的eclipse有些问题,打开无法立即获取界面,需要手动输路径,也不清楚为啥。 SpringMVC(一)——搭建环境 标签:eclips lang location dex mod 运行 方法调用 核心 startup 原文地址:https://www.cnblogs.com/shoulinniao/p/12952861.htmlxml 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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
servlet>
servlet-name>springmvcservlet-name>
servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
init-param>
param-name>contextConfigLocationparam-name>
param-value>classpath:springmvc-config.xmlparam-value>
init-param>
load-on-startup>1load-on-startup>
servlet>
servlet-mapping>
servlet-name>springmvcservlet-name>
url-pattern>/url-pattern>
servlet-mapping>
web-app>
package com.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class FirstController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response){
ModelAndView mav=new ModelAndView();
//向模型对象添加数据
mav.addObject("msg", "搭建第一个SpringMVC程序");
//设置逻辑视图名,实现页面跳转
mav.setViewName("/WEB-INF/jsp/index.jsp");
return mav;
}
}
xml version="1.0" encoding="UTF-8"?>
beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
bean name="/firstController" class="com.controller.FirstController"/>
bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
beans>
@ 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>入门程序title>
head>
body>
${msg}
body>
html>