简单mvc---模拟Springmvc
2020-12-13 05:57
标签:ring response turn charset lan version extend can col Auwowrited Controller RequestMapping RequestParam Service controller代码 service接口 service接口实现 简单mvc---模拟Springmvc 标签:ring response turn charset lan version extend can col 原文地址:https://www.cnblogs.com/JavaHxm/p/11157428.html1、注解篇
package org.aaron.mvc.annaotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AaronAutowrited {
String value() default "";
}
package org.aaron.mvc.annaotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AaronController {
String value() default "";
}
package org.aaron.mvc.annaotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AaronRequestMapping {
String value() default "";
}
package org.aaron.mvc.annaotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AaronRequestParam {
String value() default "";
}
package org.aaron.mvc.annaotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AaronService {
String value() default "";
}
2、DispatcherServlet
package org.aaron.mvc.servlet;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.aaron.mvc.annaotation.AaronAutowrited;
import org.aaron.mvc.annaotation.AaronController;
import org.aaron.mvc.annaotation.AaronRequestMapping;
import org.aaron.mvc.annaotation.AaronRequestParam;
import org.aaron.mvc.annaotation.AaronService;
import org.omg.CORBA.Request;
public class DispatcherServlet extends HttpServlet {
List
3、实战篇
package org.aaron.mvc.ctrl;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.aaron.mvc.annaotation.AaronAutowrited;
import org.aaron.mvc.annaotation.AaronController;
import org.aaron.mvc.annaotation.AaronRequestMapping;
import org.aaron.mvc.annaotation.AaronRequestParam;
import org.aaron.mvc.service.TestService;
@AaronController
@AaronRequestMapping("/Aaron")
public class TestCtrl {
@AaronAutowrited("TestServiceImpl")
private TestService service;
@AaronRequestMapping("/query")
public void query(HttpServletRequest request,HttpServletResponse response,
@AaronRequestParam("name") String name, @AaronRequestParam("age") String age){
String result = service.query(name, age);
try {
PrintWriter out;
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
out = response.getWriter();
out.write(result);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package org.aaron.mvc.service;
public interface TestService {
String query(String name,String age);
}
package org.aaron.mvc.service.impl;
import org.aaron.mvc.annaotation.AaronService;
import org.aaron.mvc.service.TestService;
@AaronService("TestServiceImpl")
public class TestServiceImpl implements TestService{
public String query(String name, String age) {
// TODO Auto-generated method stub
return "当前用户:"+name+"年龄"+age ;
}
}
上一篇:Java多线程(2)线程锁
下一篇:php执行效率相关的语句