一、项目搭建
1、可以在新建项目的使用Spring MVC框架。或者创建一个简单的项目之后再用Add Framework Support来添加Spring MVC框架。
2、删除自动生成的lib的jar包,使用pom文件来进行管理包。目录结构如下图。
3、pom文件。加载完成之后才能进行下一步。
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.zxj
zxj-spring-mvc
1.0-SNAPSHOT
zxj-spring-mvc
http://www.example.com
war
UTF-8
1.7
1.7
4.3.18.RELEASE
junit
junit
4.12
test
org.springframework
spring-test
${spring.version}
test
org.springframework
spring-context
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
javax.servlet.jsp.jstl
jstl-api
1.2
${project.artifactId}
maven-clean-plugin
3.1.0
maven-install-plugin
2.5.2
4、Project Structure的编辑,创建一下包名。
二、webapp的编辑
1、目录结构。
2、web.xml。
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
index.jsp
contextConfigLocation
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/dispatcher-servlet.xml
1
dispatcher
/
3、applicationContent.xml。
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
4、dispatcher-servlet.xml。
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.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">
5、index.jsp默认页面。
hello worldwelcome zhuoxiaojie spring mvc
6、hello.jsp。
testhello world
7、test2.jsp。
testkey1: ${key1} , key2: ${key2}
三、Controller层
package com.xiaojie.spring.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("/hello")
public ModelAndView test1(Model model) {
return new ModelAndView("hello");
}
@GetMapping("/test2")
public ModelAndView test2(Model model) {
model.addAttribute("key1", "卓小杰");
model.addAttribute("key2", "你真帅");
return new ModelAndView("test2");
}
}
四、Tomcat的配置
1、下载Tomcat8。自己去百度教程下载。
2、用Tomcat进行启动项目的配置。然后启动项目。
war模式:将web工程以war包的形式上传到服务器
war exploed模式:将web工程以当前文件夹的位置关系上传到服务器
五、测试结果
1、启动之后的默认界面index.jsp。
2、hello.jsp界面。
3、test2.jsp界面。带参数。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。