当前位置: 移动技术网 > IT编程>开发语言>Java > SpringMVC框架

SpringMVC框架

2019年04月27日  | 移动技术网IT编程  | 我要评论

一:内容

  springmvc,它属于spring基本框架里面的一个组成部分,在实际开发过程中,接收浏览器请求响应,对数据进行数据处理,然后返回页面进行显示。

 

二:入门示例:

  1.创建web工程,并导入相应的jar包

 

  2.新建springmvc全局配置文件

 

 

 <?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:mvc="http://www.springframework.org/schema/mvc"
       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-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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
 
 
</beans>

 

   3.在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>springmvc_01</display-name>
  <!-- 配置前端控制器dispatcherservlet -->
  <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:springmvc.xml</param-value>
    </init-param>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

 

  4.编写处理器handler

package cn.gzsxt.controller;
 
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
import org.springframework.web.servlet.modelandview;
import org.springframework.web.servlet.mvc.controller;
 
public class hellocontroller implements controller{
 
    @override
    public modelandview handlerequest(httpservletrequest request,
            httpservletresponse response) throws exception {
        modelandview modelview = new modelandview();
        //类似于 request.setattribute()
        modelview.addobject("name","张三");
        modelview.setviewname("/web-inf/view/index.jsp");
        return modelview;
    }
 
}

 

  5.配置handler,处理器映射器,配置处理器适配器,试图解析器

<!-- 配置handler -->   
<bean name="/hello.do" class="com.ys.controller.hellocontroller" />
   <!-- 配置处理器映射器
    将bean的name作为url进行查找,需要在配置handler时指定bean name(就是url)-->
<bean class="org.springframework.web.servlet.handler.beannameurlhandlermapping" />
   <!-- 配置处理器适配器,所有适配器都得实现 handleradapter接口 -->
   <bean class="org.springframework.web.servlet.mvc.simplecontrollerhandleradapter" />
 
<!-- 配置视图解析器
    进行jsp解析,默认使用jstl标签,classpath下得有jstl的包-->
<bean class="org.springframework.web.servlet.view.internalresourceviewresolver" />

 

  6.在web-inf/view目录下创建index.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>
hello:${name}
</body>
</html>


  7.浏览器输出:

 




如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网