《Java Spring框架》通过IDEA搭建SpringMVC
前言
目前web框架更新迅速,学习新框架的同时也有必要了解一下之前框架的内容,SpringBoot现在比较流行,今天就来通过搭建SpringMVC来了解一下SpringMVC, SpringBoot也是基于SpringMVC的扩展。
准备
IDEA2018年版本,JDK8,Tomcat7
搭建
下一步:
根据网速等一些原因,下载jar可能比较慢。
看看目录结构
提前给大家先看看该案例的完整的目录结构
这里需要注意一点:IDEA创建的SpringMVC是没有没有指定好映射关系的,需要自己添加:
然后再设置一下web.xml:
idea会提示我们需要创建一个springmvc-config.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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <context:annotation-config /> <!-- 配置自动扫描的包,完成 Bean 的创建和自动依赖注入的功能 --> <context:component-scan base-package="controller" /> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
代码
创建UserController
import dto.UserDTO; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class UserController { @RequestMapping(value="/register.from") public String Register(@ModelAttribute("form") UserDTO userDTO, Model model) { model.addAttribute("userDTO", userDTO); return "success"; } }
创建UserDTO
public class UserDTO { private Integer id; private String username; private String password; private Integer age; @Override public String toString() { return "UserDTO{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", age=" + age + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
然后创建JSP,在web目录下面
register.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>register page</title> </head> <body> <form action="register.form" method="post"> <h5>User registration</h5> <p> <label>name </label> <input type="text" id="username" name="username" tabindex="1"> </p> <p> <label>password </label> <input type="text" id="password" name="password" tabindex="2"> </p> <p> <label>age </label> <input type="text" id="age" name="age" tabindex="3"> </p> <p id="buttons"> <input id="submit" type="submit" tabindex="4" value="register"> <input id="reset" type="reset" tabindex="5" value="reset"> </p> </form> </body> </html>
success.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>success page</title> </head> <body> <h5>login was successful</h5> <p> name:${requestScope.userDTO.username}<br /> password:${requestScope.userDTO.password}<br /> age:${requestScope.userDTO.age}<br /> </p> </body> </html>
最后
新建之前正常。
使用tomcat启动程序
选择tomcat
选择你本地的tomcat
给tomcat设置上需要运行的包,ok启动
浏览器访问:
提交后:
总结:SpringMVC大大的减少我们Web项目对配置的依赖,配置大部分都被注解代替,注解是代码的一部分更加容易被开发者接收。
This moment will nap, you will have a dream; But this moment study,you will interpret a dream.