从零单排SpringMVC——SpringMVC项目搭建
来源:互联网 发布:燃烧热数据 编辑:程序博客网 时间:2024/06/14 01:24
本文使用intellij idea构建springMVC项目。
新建项目
file-new-project,选择maven项目进行管理
填写相关信息后,一路到finish。具体的操作网上相关介绍很多,不再赘述。
基于注解驱动的配置和代码
pom.xml的配置
写入spring相关的依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- spring end --> </dependencies> <build> <finalName>WebappDemo</finalName> <!--servlet容器 jetty插件--> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.10.v20160621</version> </plugin> </plugins> </build>
这里 对中加入的plugins是为了能够使用jetty进行启动。
web.xml的配置
采用最简单的配置,只配置一个servlet
<!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> <servlet> <servlet-name>WebAppDemo</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>WebAppDemo</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
- load-on-startup:表示启动容器时初始化该Servlet;
- url-pattern:表示哪些请求交给Spring Web MVC处理,
- “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。
servlet.xml的配置
然后是mvc-servlet.xml文件的配置,上面配置DispatcherServlet会默认加载[servlet-name]-servlet.xml文件。对于我的配置,会去加载WebAppDemo-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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-lazy-init="true"> <!-- 添加注解驱动 --> <mvc:annotation-driven /> <!-- 默认扫描的包路径 --> <context:component-scan base-package="com.wz.yuwan" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp"/> </bean></beans>
控制器代码
package com.wz.yuwan.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class AnnotationController { @RequestMapping(value = "/login",method = RequestMethod.GET) public ModelAndView login(){ final ModelAndView mv = new ModelAndView(); //添加模型数据 可以是任意的POJO对象 mv.addObject("message", "Hello LostShadow!"); //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面 mv.setViewName("loginview"); return mv; }}
视图view
<%-- Created by IntelliJ IDEA. User: SEELE Date: 2017/9/23 Time: 17:58 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ page isELIgnored="false" %><html><head> <title>Login</title></head><body> ${message}</body></html>
基于非注解驱动的配置
原始的mvc做法,需要控制器继承controller
pom.xml配置
添加j2ee相关依赖
<!--j2ee相关包 servlet、jsp、jstl--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
servlet.xml配置
<!-- HandlerMapping --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 处理器 --> <bean name="/hello" class="com.wz.yuwan.controller.NonAnnotationController"/>
- BeanNameUrlHandlerMapping:表示将请求的URL和Bean名字映射,如URL为 “上下文/hello”,则Spring配置文件必须有一个名字为“/hello”的Bean,上下文默认忽略。
- SimpleControllerHandlerAdapter:表示所有实现了org.springframework.web.servlet.mvc.Controller接口的Bean可以作为Spring Web MVC中的处理器。如果需要其他类型的处理器可以通过实现HadlerAdapter来解决
控制器代码
package com.wz.yuwan.controller;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class NonAnnotationController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { final ModelAndView mv = new ModelAndView(); //添加模型数据 可以是任意的POJO对象 mv.addObject("message", "Hello NonAnnotationController!"); //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面 mv.setViewName("loginview"); return mv; }}
偷懒用了同一个jsp视图
启动
设置edit configurations
浏览器访问localhost:8080/login
显示:Hello LostShadow!
启动成功
浏览器访问localhost:8080/hello
显示: Hello NonAnnotationController!
启动成功
阅读全文
0 0
- 从零单排SpringMVC——SpringMVC项目搭建
- 零配置搭建SpringMVC
- Ionic从零单排(一)——Ionic开发环境搭建
- 搞搞UITabBar——iOS从零单排
- 搞搞UITabBar——iOS从零单排
- Ionic从零单排(二)——HelloWorld
- 数据结构从零单排1——顺序表
- 数据结构从零单排2——链表(1)
- 数据结构从零单排3——链表(2)
- 从零单排1
- 从零单排2
- 从零单排3
- 从零单排4
- 从零单排5
- 从零单排6
- 从零单排7
- 从零单排8
- 从零单排9
- QT状态栏(statusbar)用法
- 应届生面试注意问题
- MySQL查询优化之explain的深入解析
- spring解析element
- HDU 1565 方格取数(1)
- 从零单排SpringMVC——SpringMVC项目搭建
- IOC基本原理(控制反转 )
- 数据结构——第一讲、基本概念(1)
- PHP实现在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- Maven编译报:编码GBK的不可映射字符与程序包org.junit.runner不存在
- MFC入门经典
- 群赛14----2017.9.24
- 计数系统,架构与优化细节
- [BZOJ1509][NOI2003]逃学的小孩(链上乱搞)