当前位置: 移动技术网 > IT编程>软件设计>面向对象 > springmvc注解@Controller和@RequestMapping

springmvc注解@Controller和@RequestMapping

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

spring从2.5版本引入注解,从而让开发者的工作变得非常的轻松

springmvc注解controller

org.springframework.stereotype.controller注解类型用于指示spring类的实例是一个控制器,使用该注解不需要继承特定的类或实现特定的接口,相比较配置文件而言更加简单。

@target({elementtype.type})
@retention(retentionpolicy.runtime)
@documented
@component
public @interface controller {

    string value() default "";

}

requestmapping注解

org.springframework.web.bind.annotation.requestmapping注解用来指示spring用哪一个类或者方法来处理请求动作,该注解可用于类或方法。当@requestmapping注解在类上时候,所有的方法都将映射为相对于类级别的请求,表示该控制器处理的所有的请求都将被映射到value属性指示的路径下
源码如下

package org.springframework.web.bind.annotation;

import java.lang.annotation.documented;
import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;
import java.util.concurrent.callable;

import org.springframework.core.annotation.aliasfor;


@target({elementtype.method, elementtype.type})
@retention(retentionpolicy.runtime)
@documented
@mapping
public @interface requestmapping {

    /**
     * 给映射地址一个别名
     */
    string name() default "";

    /**
     * 用于将指定的请求的实际地址映射到方法上
     */
    @aliasfor("path")
    string[] value() default {};

    @aliasfor("value")
    string[] path() default {};

    /**
     * 映射指定的请求的方法类型:
     * get, post, head, options, put, patch, delete, trace.
     */
    requestmethod[] method() default {};

    /**
     * 指定request中必须包含某些参数值,才让该方法处理
     */
    string[] params() default {};

    /**
     * 指定request中必须包含某些指定的header值,才让该方法处理
     */
    string[] headers() default {};

    /**
     * 指定处理请求提交的内容(content-type),例如application/json、text/html等
     */
    string[] consumes() default {};

    /**
     * 指定返回的内容类型,返回的内容类型必须是request请求头(accept)中所包含的类型
     */
    string[] produces() default {};

}

model和 modelandview

springmvc在内部使用一个org.springframework.ui.model接口存储模型数据,类似map接口。springmvc在调用处理方法之前会创建一个隐含的模型对象,作为模型数据的存储器。如果处理方法的参数为model和modelmap类型,则springmvc会将隐含模型的引用传递给这些参数。在处理方法内部,开发者就可以通过这个参数队形访问模型中的所有数据,也可以像模型中添加新的属性数据。
usercontroller.java

package com.rookie.bigdata.controller;

import com.rookie.bigdata.domain.user;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.modelattribute;
import org.springframework.web.bind.annotation.requestmapping;

/**
 * created by dell on 2019/6/15.
 */
@controller
public class usercontroller {

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

    /**
     * @param loginname
     * @param password
     * @param model
     * @modelattribute修饰的方法会先于login调用,该方法用于接受前台jsp页面传入的参数,该方法 public void usernodel(string loginname, string password, model model)可以直接改为 public void usernodel(string loginname, string password, modelmap modelmap)  
     */
    @modelattribute
    public void usernodel(string loginname, string password, model model) {
        log.info("该方法执行啦");
        //用于存储jsp页面传入的参数
        user user = new user();
        user.setloginname(loginname);
        user.setpassword(password);
        //将user添加到model中
        model.addattribute("user", user);
    }

    @requestmapping(value = "/login1")
    public string login(model model) {
        //从model中取出user对象
        user user = (user) model.asmap().get("user");
        system.out.println(user);
        //设置user对象的属性
        user.setusername("张三");
        return "result1";
    }


}

loginform1.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>测试model</title>
</head>
<body>
<h3>测试model</h3>
<form action="login1" method="post">
     <table>
         <tr>
            <td><label>登录名: </label></td>
             <td><input type="text" id="loginname" name="loginname" ></td>
         </tr>
         <tr>
            <td><label>密码: </label></td>
             <td><input type="password" id="password" name="password" ></td>
         </tr>
         <tr>
             <td><input id="submit" type="submit" value="登录"></td>
         </tr>
     </table>
</form>
</body>
</html>

modelandview

控制器处理方法的返回值如果是modelandview,则其既包含模型数据信息,也包含视图信息。
如下代码跟上面的效果相同

package com.rookie.bigdata.controller;

import com.rookie.bigdata.domain.user;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.ui.modelmap;
import org.springframework.web.bind.annotation.modelattribute;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.servlet.modelandview;

/**
 * created by dell on 2019/6/15.
 */
@controller
public class user1controller {

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

    /**
     * @param loginname
     * @param password
     * @param modelandview
     */
    @modelattribute
    public void usernodel(string loginname, string password, modelandview modelandview) {
        log.info("该方法执行啦");
        //用于存储jsp页面传入的参数
        user user = new user();
        user.setloginname(loginname);
        user.setpassword(password);
        //将user添加到model中
        modelandview.addobject("user", user);
    }

    @requestmapping(value = "/login1")
    public modelandview login(modelandview modelandview) {
        //从modelandview的model中取出user对象
        user user = (user) modelandview.getmodel().get("user");
        system.out.println(user);
        //设置user对象的属性
        user.setusername("张三");
        modelandview.setviewname("result1");
        return modelandview;
    }
}

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

相关文章:

验证码:
移动技术网