SpringMVC入门(二)SpringMVC注解方式的环境配置

前言

      本章讲解SpringMVC注解方式的环境配置

方法

1.概念

我们知道,SpringMVC其实也是有纯XML的配置的,但是就目前的发展态势来看,企业更偏向于更加简单的注解开发,所以各大框架主推的就是注解的开发,为了节约时间,我这里直接就上注解的环境配置了!

当然了,首先还是要导入springMVC的相关jar包,也就是spring-webmvc.jar

2.配置SpringMVC的分发器DispatcherServlet

我们知道SpringMVC有四大组件,其中最重要的就是DispatcherServlet了,它是SpingMVC用来收集用户发送的请求,进行分配,发送至HandlerMapping来确定执行的方法。

那么如何来配置呢?它和普通的配置servlet没有什么区别,在web.xml进行如下配置即可:

<!-- 配置DispatcherServlet -->
<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

其中需要重点配置SpringMVC的全局配置文件路径和该servlet的启动顺序,拦截所有请求写上“/”

3.配置SpringMVC的配置文件——spring-servlet.xml

在这个配置文件中,我们只需要配置HandlerMapping、HandlerAdapter、ViewResolver这三大组件

新建spring-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: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
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 过滤静态资源文件 -->
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>

    <!-- 启用springMVC相关注解 -->
    <context:annotation-config/>
    <!-- 启用springMVC注解扫描路径 -->
    <context:component-scan base-package="cn.edu.ccut.controller"></context:component-scan>
    <!-- 配置注解驱动,相当于配置了handlerMapping和handlerAdapter -->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 配置视图解析器 -->
    <bean id="viewResolverJsp" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

有的人可能会问,在applicationContext.xml中已经扫描了包,为什么还要扫描?

之前已经说过,spring无法管理springMVC的相关注解,所以它的扫描自然没有作用,需要在springMVC配置文件中重新配置注解扫描

4.编写控制器代码

官方建议控制器的包名为XX.controller

所以我们之前的servlet包名就不要用了!

新建StudentController.java,进行如下编写:

package cn.edu.ccut.controller;

import cn.edu.ccut.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @RequestMapping("/login")
    public String login(String username,String password){
        return "/admin/list/welcome";
    }
}

注解解释:

@Controller:这个不用多说,用来表示这个类是一个控制器类,于@Component功能一致

@Autowired:这个前面也做过解释,它代表按byType的方式进行依赖注入

@RequestMapping("路径"):该注解表示该controller的访问路径以及每一个方法的具体路径

一般的,使用注解方式配置controller的好处在于其方法可以任意的进行定义

如上面所示的那样,返回值是String类型的一般情况下就是进行一个页面的跳转!

这里我们可以发现,它跳转的是/admin/list/welcome.jsp

至于为什么没有写.jsp呢?细心的同学就会发现,在springMVC的配置文件中我配置了视图解析器,它标明了跳转的页面的后缀为.jsp,所以我们只需要写除了.jsp以外的路径就可以了!

5.在jsp文件中调用该controller的方法

这里我们展示的是SMS的登录功能:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	request.setAttribute("path", request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/");
%>
<!DOCTYPE HTML>
<html>

	<head>
		<title>用户登录</title>
		<base href="${requestScope.path}">
		<script type="text/javascript" src="js/student.js"></script>
		<link type="text/css" href="css/form.css" rel="stylesheet" />
	</head>

	<body>
		<div style="border-left:100px solid white;text-align: left;">欢迎使用学生信息管理系统^_^</div>
		<div id="header" style="border-right:100px solid white;border-left:100px solid white;">
			<img src="images/header.jpg" height="60px" width="1100px" />
		</div>
		<div style="background-color:aqua;border-left:100px solid white;border-right:150px solid white;height: 400px;width: 1100px;">
			<div style="height: 100px;"></div>
			<div style="border-left:400px solid aqua;float: left;height:300px;"></div>
			<div style="text-align: center;float: left;height:300px;">
				<form action="student/login" method="post" onSubmit="return validate()">
					<table border="1">
						<tr>
							<td colspan="3"><strong>用户登录</strong></td>
						</tr>
						<tr>
							<td>用户名:</td>
							<td><input type="text" name="username" id="username" class="init" onBlur="validateName()"></td>
							<td><span id="usernameMsg">&nbsp;&nbsp;&nbsp;&nbsp;</span></td>
						</tr>
						<tr>
							<td>密&nbsp;&nbsp;码:</td>
							<td><input type="text" name="password" id="password" class="init" onBlur="validatePass()"></td>
							<td><span id="passwordMsg">&nbsp;&nbsp;&nbsp;&nbsp;</span></td>
						</tr>
						<tr>
							<td colspan="3"><input type="submit" value="登陆"> <input type="reset" value="重置"></td>
						</tr>
					</table>
				</form>
				<h3 style="text-align: center;color: red">${requestScope.errorMsg}</h3>
			</div>
		</div>
		<div id="footer" style="border-right:100px solid white;border-left:100px solid white;">
			<img src="images/footer.jpg" height="60px" width="1100px" />
		</div>
		<div style="border-right:200px solid white;text-align: right;width: 1100px;">版权所有&nbsp;www.wangjian.com</div>
	</body>

</html>

我们可以发现,表单提交的action恰好是我们controller配置的requestMapping中的路径!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值