Giter VIP home page Giter VIP logo

amvc's Introduction

amvc

已经可以用了~~~

目前只能返回json类型,视图部分还没有做。

开发记录

有兴趣的看一下吧

  1. 自己写一个mvc框架吧(一)
  2. 自己写一个mvc框架吧(二)
  3. 自己写一个mvc框架吧(三)
  4. 自己写一个mvc框架吧(四)
  5. 自己写一个mvc框架吧(五)

怎么用呢?

1:在web.xml中添加:

<servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>com.hebaibai.amvc.MvcServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

2:在 resources 中添加配置文件 mvc.json

{
  "annotationSupport": true,//开启注解支持
  "annotationPackage": "com.hebaibai.demo.web"//要被扫描的包名
}

3:在 class 上添加注解 @Request

4:在 方法 上添加注解。

import com.hebaibai.amvc.annotation.Request;

/**
 * @author hjx
 */
@Request("index")
public class IndexController {

    @Request(value = "/test/info")
    public String testGet() {
        return "success";
    }

}

5:启动项目,访问:http://127.0.0.1:8080/[项目名称]/index/test/info 就好了。

指定请求方式

//get
@Request(value = "test", type = RequestType.GET)
public String testGet() {
    return "success";
}
//post
@Request(value = "test", type = RequestType.POST)
public String testPost() {
    return "success";
}
//put
@Request(value = "test", type = RequestType.PUT)
public String testPut() {
    return "success";
}
//delete
@Request(value = "test", type = RequestType.DELETE)
public String testDelete() {
    return "success";
}

接收参数

1:基本形式:

支持基本类型的数据自动转换。

参数名称为方法入参的名称。

//get请求
//test?name=何白白&age=20
@Request(value = "test", type = RequestType.GET)
public String testGet(String name,int age) {
	return "success";
}
//post put get一样

2:路径传参

路径中使用占位符 {},占位符中的名称要和参数名称对应。

@Request(value = "user/{name}/{age}", type = RequestType.GET)
public String user(String name, int age) {
    return "success";
}

与Ioc框架集成

1:写一个类继承 MvcServlet.class

import com.hebaibai.amvc.Application;
import com.hebaibai.amvc.MvcServlet;
import com.hebaibai.amvc.objectfactory.ObjectFactory;

public class MyMvc extends MvcServlet {

    /**
     * 执行其他内容
     * @param application
     */
    @Override
    protected void afterInitMvc(Application application) {
        application.setObjectFactory(new ObjectFactory() {
            @Override
            public Object getObject(Class objectClass) {
                //返回通过ioc框架实例化的对象
                object
                return object;
            }
        });
    }
}

修改 web.xml

<servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>写你自己实现的,继承了MvcServletde的类名</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

amvc's People

Contributors

hjx601496320 avatar quietclickcode avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.