JAVA之初识springMVC框架
2017-08-14 16:45
323 查看
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文件中添加配置:
监听器:
分发器
在这个配置中,我们规定了 DispatcherServlet 的关联 XML 文件名称叫做 dispatcher-servlet,但是这里的路径是相对于web.xml来说的,也就是说,这个文件也在WEB-INF的根目录下。
所以,我们需要在WEB-INF的根目录下新建一个dispatcher-servlet.xml文件,添加以下代码:
(注:将代码中的对应工程名替换为自己的)
css , images 都需要放在/resources目录下,这个目录现在我们还没有建。所有的展示页面,比如jsp文件,都需要放置在/WEB-INF/pages目录下。
5、新建包
在Java Resources/src 下新建controller.java
6、新建文件夹
建立resources、pages文件夹,结果如下:
7、在pages下面建立xxxx.jsp文件用作测试,写入代码:
8、导入spring框架需要的jar包,将jar包放入lib文件夹:
9、添加SpringBean工厂的配置文件applicationContext.xml:
写入代码:
10、编写测试代码
在controller下新建class :TestController.java,写入代码:
11、将项目加入到tomcat,并启动
启动完成后,输入http://localhost:8080/SpringTest/Project/Test?page=xxxx ,步骤正确的话应该显示如下图:
附:spring jar包下载地址:http://download.csdn.net/download/w350981132/9933304
操作系统:Mac OS 10.12.6
Tomcat : v7.0
JDK : 1.7
工具:eclipse
2、新建项目
用eclipse新建项目,选择Dynamic Web Project:
将项目字符集改为UTF-8:
在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.java
6、新建文件夹
建立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
相关文章推荐
- 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
- 一步步学java框架之Spring-SpringMVC-Hibernate在idea的使用(一)
- java ssm框架学习——SpringMVC--1
- 【新技术】HTML5 全新高大尚后台框架 bootstrap springmvc mybatis java企业级通用权限安全框架源码
- 【Java Web】MVC(一)初识SpringMVC
- 初识Java 开源框架 Play
- 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
- 集成springmvc和mybatis环境的Javaweb基础框架,省去新手搭建繁琐环境的苦恼
- java项目案例 java项目源码 java后台框架源码 企业级 SpringMVC mybatis or hibernate
- 集成springmvc和mybatis环境的Javaweb基础框架,省去新手搭建繁琐环境的苦恼
- java后台框架 springmvc mybatis oracle 和 mysql数据库
- java后台框架 springmvc mybatis(sqlsever oracle 和 mysql数据库) HTML5 bootstrap 全新高大尚
- java表现层框架struts2和SpringMVC获取页面参数的方式
- 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
- 《Java从入门到放弃》框架入门篇:springMVC基本用法
- Java-springMVC框架:springMVC简单搭建一
- 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
- Java框架之Hibernate--初识Hibernate
- 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
- 【新技术,学习、商业均可】HTML5 全新高大尚后台 bootstrap springmvc mybatis java企业级通框架源码入门