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

springmvc框架helloword

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

spring框架提供了构建web的应用程序的全功能mvc模块-spring mvc。我们首先来写一个springmvc的hellword的配置文件的形式

工程结构如下

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>欢迎页面</title>
</head>
<body>
<!-- 页面可以访问controller传递传递出来的message,requestscope:表示变量能在本次请求中使用 -->
${requestscope.message}
</body>
</html>

requestscope:表示变量的作用域,一共4种。pagescope: 表示变量只能在本页面使用。requestscope:表示变量能在本次请求中使用。sessionscope:表示变量能在本次会话中使用。applicationscope:表示变量能在整个应用程序中使用

web.xml配置

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="webapp_id" version="3.1">
  <!-- 定义spring mvc的前端控制器 -->
  <servlet>
    <servlet-name>helloword</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.dispatcherservlet
    </servlet-class>
    <init-param>
      <param-name>contextconfiglocation</param-name>
      <param-value>config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 让spring mvc的前端控制器拦截所有请求 -->
  <servlet-mapping>
    <servlet-name>helloword</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

config.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"
    xsi:schemalocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
       
    <!-- 配置handle,映射"/hello"请求 -->
    <bean name="/hello" class="com.rookie.bigdata.hellocontroller"/>

    <!-- 处理映射器将bean的name作为url进行查找,需要在配置handle时指定name(即url) -->
    <bean class="org.springframework.web.servlet.handler.beannameurlhandlermapping"/>

    <!-- simplecontrollerhandleradapter是一个处理器适配器,所有处理适配器都要实现 handleradapter接口-->
    <bean class="org.springframework.web.servlet.mvc.simplecontrollerhandleradapter"/>
    
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"/>

</beans>

hellocontroller.java

package com.rookie.bigdata;

import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import org.springframework.web.servlet.modelandview;
import org.springframework.web.servlet.mvc.controller;


public class hellocontroller implements controller {
    private static final log logger = logfactory
            .getlog(hellocontroller.class);

    /**
     * handlerequest是controller接口必须实现的方法。
     * 该方法的参数是对应请求的httpservletrequest和httpservletresponse。
     * 该方法必须返回一个包含视图路径或视图路径和模型的modelandview对象。
     */
    @override
    public modelandview handlerequest(httpservletrequest request,
                                      httpservletresponse response) throws exception {
        logger.info("handlerequest 被调用");
        // 创建准备返回的modelandview对象,该对象通常包含了返回视图的路径、模型的名称以及模型对象
        modelandview mv = new modelandview();
        // 添加模型数据 可以是任意的pojo对象
        mv.addobject("message", "hello world!");
        // 设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面
        mv.setviewname("/index.jsp");
        // 返回modelandview对象。
        return mv;
    }

}

在idea中启动该程序,在浏览器访问即可

helloword的注解形式

spring2.5版本开始新增了可基于注解的控制器,控制器不需要实现controller接口,通过注解即可达到上面的要求

web.xml文件

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="webapp_id" version="3.1">
  <!-- 定义spring mvc的前端控制器 -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.dispatcherservlet
    </servlet-class>
    <init-param>
      <param-name>contextconfiglocation</param-name>
      <param-value>/config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 让spring mvc的前端控制器拦截所有请求 -->
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

hellocontroller

package com.rookie.bigdata;

import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.servlet.modelandview;

/**
 * created by dell on 2019/6/14.
 */
@controller
public class hellocontroller {

    private static final log logger = logfactory.getlog(hellocontroller.class);

    @requestmapping(value = "/hello")
    public modelandview hello() {
        logger.info("该方法调用");
        modelandview modelandview = new modelandview();
        modelandview.addobject("message", "helloword");
        modelandview.setviewname("web-inf/welcome.jsp");

        return modelandview;
    }


}

config.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:context="http://www.springframework.org/schema/context"
       xsi:schemalocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
        如果扫描到有spring的相关注解的类,则把这些类注册为spring的bean -->
    <context:component-scan base-package="com.rookie.bigdata"/>

    <!-- 配置处理映射器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.requestmappinghandlermapping"/>

    <!-- 配置处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter"/>

    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"/>

</beans>

welcome.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>welcome</title>
</head>
<body>
<!-- 页面可以访问controller传递传递出来的message -->
${requestscope.message}
</body>
</html>

启动应用程序,在浏览器访问可以出现相同的效果

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

相关文章:

验证码:
移动技术网