本项目旨在搭建一个简单的Spring MVC框架,了解Spring MVC的基础配置等内容。
一、项目结构本项目使用idea intellij创建,配合maven管理。整体的目录结构如图:
其中java文件夹是sources文件夹,resources是资源文件夹。spring文件夹里是Spring上下文配置和Spring MVC配置文件。
需要注意的是,项目自动生成以后会有两个
web文件目录,一个是web文件夹(我这里已经删除了),另一个是默认的webapp文件夹。我这里使用默认的目录,也就是说webapp文件夹。页面文件都放在该目录里面。
*如图可配置默认的web文件默认路径。*
二、配置文件maven配置,添加各种依赖省略,这个不是重点。需要注意的是,添加依赖的时候不能重复添加不同版本的包。
web.
首先,添加spring 上下文配置,指定Spring Context由classpath下的spring文件夹中的app-context.<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/app-context.</param-value></context-param>
然后添加Spring**:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener><listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener>
接下来配置Spring MVC的dispatherservlet,同时配置该servlet要拦截的URL。
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/mvc-servlet.</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet><!-- 配置要拦截的URL --><servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern></servlet-mapping>
最后,配置一个welcom-file-list。
web.<??><web-app="http://java.sun.com/="http://www.w3.org/2001/version="2.4"xsi:schemaLocation="http://java.sun.com/> <!-- spring context 配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/app-context.</param-value> </context-param> <!-- spring **配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <!--spring 防内存溢出** --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- spring mvc servlet配置文件 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/mvc-servlet.</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置要拦截的URL --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file></welcome-file> </welcome-file-list></web-app>
配置Spring MVC文件
这里主要配置自动注解、mvc资源引用、视图解析器等
代码如下:
<??><beans ="http://www.springframework.org/schema/beans" ="http://www.w3.org/2001/ ="http://www.springframework.org/schema/mvc" ="http://www.springframework.org/schema/context" 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 "> <mvc:resources location="/js/" mapping="/js/**"/> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <mvc:annotation-driven/> <context:annotation-config/> <mvc:default-servlet-handler/> <!--添加component扫描,使package下面的注解生效 --> <context:component-scan base-package="com.wxspringmvc.controller"/> <!--添加页面视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/"/> <property name="suffix" value=".jsp"/> <property name="contentType" value="text/html;charset=UTF-8"/> </bean></beans>
配置applicationContext.
这里主要是提供默认的上下文,不需要额外的配置,直接使用生成的文件就可以。代码就省略了。
要注意的是,applicationContext.View和Controller
这里使用一个简单的用户登录,完成后在页面显示用户名和密码的流程来展示。
View:index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>请登录</title></head><body><h5>this is index.jsp</h5><form action="/user/index" method="post"> <p>用户名:</p><input type="text" id="username" name="username"> <p>密码:</p><input type="password" id="password" name="password"> <p><input type="submit" value="提交"></p></form></body></html>
表单使用post的方式提交到/user/index路径。
Controller:UserController.java
@Controller@RequestMapping(value = "/user")public class UserController { @RequestMapping(value = "/index" ,method= RequestMethod.POST) public ModelAndView userIndex(String username,String password){ ModelAndView mav = new ModelAndView("user/success"); mav.addObject("username",username); mav.addObject("password",password); return mav; }}
这里可以添加一个简单的校验,如果用户名和密码有一个为空,则不能提交:
修改UserController.java的代码如下:
@Controller@RequestMapping(value = "/user")public class UserController { @RequestMapping(value = "/index" ,method= RequestMethod.POST) public ModelAndView userIndex(String username,String password){ ModelAndView mav = new ModelAndView("user/success"); if(!matchParams( username, password)){ return new ModelAndView("/index"); } mav.addObject("username",username); mav.addObject("password",password); return mav; } private boolean matchParams(String username,String password){ if(isEmpty(username)||isEmpty(password)) return false; else return true; } private boolean isEmpty(String s){ if(s==null || "".equals(s)) return true; else return false; }}
View:登录成功界面:success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html> <head> <title>用户首页</title> </head> <body><p>用户名:${username}</p><p>密码:${password}</p> </body></html>
效果展示
默认页:
输入不正确,提交结果:
输入正确提交:
----------------------------------------------------------------------------------------------------------------------------
以上就是一个简单的Spring MVC演示了。
原标题:Spring MVC篇一、搭建Spring MVC框架
关键词:Spring