Spring MVC 第二章:

eclipse的配置:

1.需要配置eclipse的快捷按钮,能让你敲代码的时候事半功倍。

可以直接导入eclipse的“Eclipse个人喜欢配置.epf”

2.配置Maven,需要在本地安装maven,配置好环境变量,设置好settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    
    <pluginGroups />
    <proxies />
    <servers />
    
    <!-- 本地仓库的位置 -->
    <localRepository>${user.home}/.m2/repository</localRepository>
    
    <mirrors>
        <mirror>
            <id>alimaven</id>
            <mirrorOf>central</mirrorOf>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
        </mirror>
        <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
        <mirror>
            <id>central</id>
            <name>Maven Repository Switchboard</name>
            <url>http://repo1.maven.org/maven2/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
        <mirror>
            <id>repo2</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo2.maven.org/maven2/</url>
        </mirror>
        <mirror>
            <id>ibiblio</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
        </mirror>
        <mirror>
            <id>jboss-public-repository-group</id>
            <mirrorOf>central</mirrorOf>
            <name>JBoss Public Repository Group</name>
            <url>http://repository.jboss.org/nexus/content/groups/public</url>
        </mirror>
        <mirror>
            <id>google-maven-central</id>
            <name>Google Maven Central</name>
            <url>https://maven-central.storage.googleapis.com
            </url>
            <mirrorOf>central</mirrorOf>
        </mirror>
        <!-- 中央仓库在中国的镜像 -->
        <mirror>
            <id>maven.net.cn</id>
            <name>oneof the central mirrors in china</name>
            <url>http://maven.net.cn/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>
    
</settings>

 3.JDK安装以及配置环境变量

4.mySQL的安装以及配置环境变量

5.tomcat的安装以及配置环境变量

 

前端的请求流程: 

 

第一步:首先,我们需要在web.xml中配置我们的前端控制器:DispatcherServlet。 在xml中,敲出dispa+/可以一键模板生成。

该前端控制器适用哪些前端页面呢,这时候就用到了我们的“处理器映射器”,就是需要配置我们的前端映射<servlet-mapping>

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>location</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<!-- 适用于哪些URL? /  代表所有URL -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

 

第二步:在web.xml中我们配置了控制器<servlet-name>SpringMVC</servlet-name>

名字叫做SpringMVC , 所以我们还需要根据这个名字来再创建一个配置文件,名称叫做“SpringMVC-servlet.xml”

这样子做的话,系统在调用web.xml配置文件的时候,也会根据<servlet-name>自动调用“SpringMVC-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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
	 http://www.springframework.org/schema/aop
	 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
	 http://www.springframework.org/schema/context
	 http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 配置扫描的包 -->
	<context:component-scan base-package="com.hello"/>
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前端资源都放在哪个目录 -->
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<!-- 以哪些结尾 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans>

注意:配置视图解析器  

当返回ModelAndView对象时,也会返回你需要登录或者跳转的页面是哪个页面,比如返回“holle” ,那也会通过SpringMVC-servlet.xml     配置好的视图解析器会自动加上后缀 .jsp ,登录 "holle.jsp"页面。

 

对于控制器的返回,我们有三种基本方式:

第一种就是直接返回静态页面

package com.hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("h")
public class HelloWorldController {
    //第一种:直接返回静态页面
	@RequestMapping("hellow")
	public String hai() {
		return "hello";
	}

    // 给model设置属性,能够在前端进行展示
	@RequestMapping("hello")
	public String helloWorld(Model model) {
		String str = "Welcome!";
		model.addAttribute("message", str);
		return "hello";
	}
	
    // 返回modelandview
	@RequestMapping("helloworld")
	public ModelAndView hello() {
		ModelAndView modelAndView = new ModelAndView();
		String str = "Welcome!";
		modelAndView.addObject("message", str);
		modelAndView.setViewName("hello");
		return modelAndView;
	}
	
	
}

前端jsp页面:

<%@ page isELIgnored="false" language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h2>Hello World!.......</h2>
	${message}
</body>
</html>

可以用${message}来进行直接展示modelandview传过来的内容

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逼哥很疯狂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值