JAVA之初识springMVC框架
来源:互联网 发布:网络语苏是什么意思 编辑:程序博客网 时间:2024/05/17 02:47
1.环境
操作系统:Mac OS 10.12.6
Tomcat : v7.0
JDK : 1.7
工具:eclipse
2、新建项目
用eclipse新建项目,选择Dynamic Web Project:将项目字符集改为UTF-8:
3、编辑web.xml
在WEB-INF下新建web.xml文件,并简单编辑(若新建项目后就存在web.xml文件,则不用再次新建:
4、集成SpringMVC
在web.xml文件中添加配置:
监听器:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener>过滤器:
<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>
分发器
<servlet> <servlet-name>springmvc</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</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
在这个配置中,我们规定了 DispatcherServlet 的关联 XML 文件名称叫做 dispatcher-servlet,但是这里的路径是相对于web.xml来说的,也就是说,这个文件也在WEB-INF的根目录下。
所以,我们需要在WEB-INF的根目录下新建一个dispatcher-servlet.xml文件,添加以下代码:
(注:将代码中的对应工程名替换为自己的)
<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.SpringTest.*"></context:component-scan> <!-- 静态资源过滤 --> <mvc:resources location="/resources/" mapping="/resources/**"/> <!-- 视图渲染 jsp/freemaker/velocity--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 制定页面存放的路径 --> <property name="prefix" value="/WEB-INF/pages/"></property> <!-- 文件的后缀 --> <property name="suffix" value=".jsp"></property> </bean> </beans>按照配置,需要注意的是,但凡是遇到有注解的,比如@Controller , @Service , @Autowired ,就会将它们加入到Spring的bean工厂里面去。而所有的静态资源文件,比如说 js , css , images 都需要放在/resources目录下,这个目录现在我们还没有建。所有的展示页面,比如jsp文件,都需要放置在/WEB-INF/pages目录下。
5、新建包
在Java Resources/src 下新建controller.java6、新建文件夹
建立resources、pages文件夹,结果如下:
7、在pages下面建立xxxx.jsp文件用作测试,写入代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!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=UTF-8"><title>Insert title here</title></head><body>这是一个基于springmvc框架的demo!</body></html>
8、导入spring框架需要的jar包,将jar包放入lib文件夹:
9、添加SpringBean工厂的配置文件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>
10、编写测试代码
在controller下新建class :TestController.java,写入代码:
import javax.servlet.http.HttpServletRequest;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;@Controller@RequestMapping("/Project")public class ViewController {@RequestMapping(value = "/Test", method = RequestMethod.GET) public ModelAndView view(HttpServletRequest request){ String path = request.getParameter("page") + ""; ModelAndView mav = new ModelAndView(); mav.setViewName(path); return mav; }}
11、将项目加入到tomcat,并启动
启动完成后,输入http://localhost:8080/SpringTest/Project/Test?page=xxxx ,步骤正确的话应该显示如下图:
附:spring jar包下载地址:http://download.csdn.net/download/w350981132/9933304
阅读全文
1 0
- JAVA之初识springMVC框架
- Java之spring框架初识
- java之spring框架初识
- java基础之集合框架初识
- Java框架之Hibernate--初识Hibernate
- SpringMVC 学习系列 (1) 之 初识SpringMVC
- 初识java集合框架
- 初识Java RMI框架
- Java Spring框架初识
- Java框架学习笔记——初识Spring之简述
- WebMVC框架 之 SpringMVC
- 框架之SpringMVC
- SpringMVC之框架概述
- SpringMVC之框架概述
- javaweb框架之springmvc
- SpringMVC框架之HelloWorld
- java-maven-SpringMVC框架
- Java EE - spring框架初识
- 进制表示 & 文件权限解读
- IDE控制台打印代码链接
- UVA
- 微信公众号开发注意事项
- ADTF(Assist Automotive Data and Time-Triggered Framework)介绍
- JAVA之初识springMVC框架
- c++中不知道的事儿
- STM32L151 PWM输出示例
- 几年级学奥数最好?奥数金牌教练建议3年级开始学...
- 补提交卡 HihoCoder
- 机器学习实战_初识朴素贝叶斯算法_理解其python代码(一)
- PAT题解——1051. Pop Sequence (25)
- 度度熊保护村庄 HDU
- Mybatis的SqlSession