首页 > 学院 > 开发设计 > 正文

一个简单的SpringMVC程序

2019-11-06 06:27:17
字体:
来源:转载
供稿:网友

java Web 开发的你,一定听说过SPRingMVC的大名,作为现在运用最广泛的Java框架,它到目前为止依然保持着强大的活力和广泛的用户群。

本文介绍如何用eclipse一步一步搭建SpringMVC的最小系统,所谓最小系统,就是足以使项目在SpringMVC框架下成功跑起来,并且能够做一些简单的事情(比如访问页面)的系统。 项目目录

jar包 所需要的jar包 下面直接给出配置及源码

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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd "></beans>

dispatcher-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 开启注解模式驱动 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 扫包 --> <context:component-scan base-package="com.springmvc.*"></context:component-scan> <!-- 静态资源过滤器 --> <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources> <!-- 视图渲染 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 定制页面存放的路径 --> <property name="prefix" value="/WEB-INF/pages/"></property> <!-- 文件的后缀 --> <property name="suffix" value=".jsp"></property> </bean> <!-- 1. 它会扫描 com.springmvc 包下所有的Java类,但凡是遇到有注解的,比如@Controller , @Service , @Autowired ,就会将它们加入到Spring的bean工厂里面去。 2. 所有的静态资源文件,比如说 js , CSS , images 都需要放在/resources目录下, 这个目录现在我们还没有建。 3. 所有的展示页面,比如jsp文件,都需要放置在/WEB-INF/pages目录下, 这个目录现在我们也没有建。 --> </beans>

web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 这是项目的名称 --> <display-name>SpringMVC_First</display-name> <!-- 配置监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 配置过滤器,解决POST乱码问题 --> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置SpringMVC 分发器,拦截所有请求 --> <servlet> <servlet-name>SpringMVC_First</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>namespace</param-name> <param-value>dispatcher-servlet</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>SpringMVC_First</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>

index.jsp源码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html> <head> <meta charset="UTF-8" /> </head> <style> body { background:url(${contextPath}/resources/img/bg.jpg); background-size : 80% 80%; } </style> <body> This is my JSP page. <br> <br/><br/> 辰小狼睡不醒------------^_^ <br/><br/> </body></html>

ViewController.java源码

package com.springmvc.controller;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;/** * @author :辰 * E-mail: 15538323378@163.com * 创建时间:2017-3-6 下午5:34:24 * */@Controllerpublic class ViewController { @RequestMapping("/view") public ModelAndView view(HttpServletRequest request){ String path = request.getParameter("path")+""; ModelAndView mav =new ModelAndView(); String contextPath = request.getContextPath(); mav.addObject("contextPath", contextPath); mav.setViewName(path); return mav; }}

在浏览器中输入http://localhost:8080/SpringMVC_First/view?path=index 就可以显示index.jsp 源码 以及背景图 一张可爱的图


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表