ssm-03-spring-mvc-02-simple-login
尝试使用spring mvc来实现简单的登录功能,pom依赖这里不一一列出来。
搭建Hello World.jsp:
- 新建maven工程:
一直点击下一步就行,此后功能文件夹结构只有一个webapp
文件夹。- 配置smart tomcat:
要注意配置时,部署目录是项目所在的webapp目录。- 运行空项目,验证tomcat配置:
- 在浏览器中输入:http://localhost:8080/index.jsp,成功呈现下图结果:
搭建Hello World controller:
- 创建java、resources和test目录并分别标记为Sources、Resources、Tests:
- resources目录下创建Spring配置文件:applicationContext.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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--自动扫描spring注解的组件,避免臃肿的bean配置--> <!--注解组件包括:@Component, @Repository, @Service, @Controller, @RestController, @ControllerAdvice, @Configuration--> <!--base-package:包含有注解组件的包名--> <context:component-scan base-package="com.zx.demo"/> <!--注解驱动,spring mvc专属配置,主要注册了dispatcher所需的RequestMappingHandlerMapping和RequestMappingHandlerAdapter--> <mvc:annotation-driven/> </beans>
- java目录下新建Controller:
package com.zx.demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotati