您的位置:首页 > 编程语言 > Java开发

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文件中添加配置:
监听器:
<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐