SpringMVC搭建+实例
想做一点自己喜欢的东西,研究了一下springMVC,所以就自己搭建一个小demo,可供大家吐槽。
- 先建一个WEB工程,这个相信大家都会,这里不在多说。
- 去网上下载spring jar包,然后在WEB-INF下新建一个lib文件,将下载的jar包放进去。如下图
- 在WEB/INF下建一个web.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springMVCtest</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springMVCtest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVCtest</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 加载spring的xml配置文件到 spring的上下文容器中 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>
注意:servlet-name的名字必须和接下来要创建的servlet.xml的名字要对应。 context-param指定applicationContext.xml 文件的位置。
- 在WEB-INF下新建一个xml,因为我上文中定义的名字是springMVCtest,所以,文件名字为springMVCtest-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:task="http://www.springframework.org/schema/task" 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-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd "> <!-- 使用默认的注解映射 <mvc:annotation-driven /> <mvc:resources location="/" mapping="/index.html" /> <context:annotation-config></context:annotation-config>--> <!-- 自动扫描controller包中的控制器 --> <context:component-scan base-package="com.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/index.do">controllerDoem</prop> </props> </property> </bean> <bean id="controllerDoem" class="com.controller"> <property name="view"> <value>index</value> </property> </bean>--> </beans>
- 在src目录下新建文件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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> </beans>
- 在src下新建一个包名,com.controller,新建一个类DemoController,注:这里的类名必须已Controller结尾,之前我写其他的类名,然后项目一直404,后来改成这样之后就好了,具体原因,还没搞清楚,知道的大神欢迎回复。
package com.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class DemoController { @RequestMapping("/index") public ModelAndView index(){ //创建模型跟视图,用于渲染页面。并且指定要返回的页面为index页面 ModelAndView mav = new ModelAndView("index"); return mav; } }
- 最后一步,在WEB-INF下新建目录jsp,里面新建文件index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>hahahaha</h1> </body> </html>
- 将项目部署到tomcat下,启动tomcat,就可以运行了,访问http://localhost:8080/springMVCtest/index
最后配上整个项目结构:
搞定!(如有错误,请大神指出。)
技术小白,努力学习。