簡介 today-web是一個基于Servlet的高性能輕量級Web框架。
安裝 <dependency><groupId>cn.taketoday</groupId><artifactId>today-web</artifactId><version>2.3.6.RELEASE</version>
</dependency>Maven Central today-web?search.maven.org
快速入門
選擇Maven項(xiàng)目
選擇類型
設(shè)置坐標(biāo)
項(xiàng)目結(jié)構(gòu)
<dependency><groupId>cn.taketoday</groupId><artifactId>today-web</artifactId><version>2.3.6.RELEASE</version>
</dependency><dependency><groupId>cn.taketoday</groupId><artifactId>today-context</artifactId><version>2.1.5.RELEASE</version>
</dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.28</version>
</dependency>
到此項(xiàng)目建立完畢,并不需要去管 web.xml 文件
/*** @author TODAY <br>* 2018-12-02 22:30*/
@RestController
@RequestMapping("index")
public class IndexController {@GET("{key}")public String index(@PathVariable String key) {return key;}@GET@ResponseBody(false)public String index() {return "index";}
}
使用模板引擎渲染頁面
Freemarker模板
部署到Tomcat
@GET({key})
@GET
案例 TAKETODAY/today-web-demo?github.com
使用說明 通過 @Controller @RestController 配置控制器 //@Controller
@RestController
@RequestMapping("/users")
public class IndexController {}
@GET("index")
@POST("post")
@PUT("articles/{id}")
......
@RequestMapping("/users/{id}")
@RequestMapping(value = "/users/**", method = {RequestMethod.GET})
@RequestMapping(value = "/users/*.html", method = {RequestMethod.GET})
@RequestMapping(value = {"/index.action", "/index.do", "/index"}, method = RequestMethod.GET)
@Interceptor({LoginInterceptor.class, ...})
public (String|List<?>|Set<?>|Map<?>|void|File|Image|...) w+ (request, request, session,servletContext, str, int, long ,byte, short, boolean, @Session("loginUser"),@Header("User-Agent"), @Cookie("JSESSIONID"), @PathVariable("id"), @RequestBody("users"), @Multipart("uploadFiles") MultipartFile[]) {service...return </>;
}
@ParameterConverter
public class DateConverter implements Converter<String, Date> {@Overridepublic Date doConvert(String source) throws ConversionException {...}
}
可以通過xml文件配置簡單視圖,靜態(tài)資源,自定義視圖解析器,文件上傳解析器,異常處理器,參數(shù)解析器
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Web-Configuration PUBLIC "-//TODAY BLOG//Web - Configuration DTD 2.0//CN""https://taketoday.cn/framework/web/dtd/web-configuration-2.3.3.dtd"><Web-Configuration><controller prefix="/error/"><action resource="400" name="BadRequest" status="400" /><action resource="403" name="Forbidden" status="403" /><action resource="404" name="NotFound" status="404" /><action resource="500" name="ServerIsBusy" status="500" /><action resource="405" name="MethodNotAllowed" status="405" /></controller><controller><action resource="redirect:http://pipe.b3log.org/blogs/Today" name="today-blog-pipe" /><action resource="redirect:https://taketoday.cn" name="today" /><action resource="redirect:https://github.com" name="github" /><action resource="redirect:/login" name="login.do" /></controller><controller class="cn.taketoday.web.demo.controller.XMLController" name="xmlController" prefix="/xml/"><action name="obj" method="obj" /><action name="test" resource="test" method="test"/></controller></Web-Configuration>
@RequestMapping(value = {"/download"}, method = RequestMethod.GET)
public File download(String path) {return new File(path);
}
@GET("/display")
public final BufferedImage display(HttpServletResponse response) throws IOException {response.setContentType("image/jpeg");return ImageIO.read(new File("D:/taketoday.cn/webapps/upload/logo.png"));
}@GET("captcha")
public final BufferedImage captcha(HttpServletRequest request) throws IOException {BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);Graphics graphics = image.getGraphics();graphics.setColor(Color.WHITE);graphics.fillRect(0, 0, IMG_WIDTH, IMG_HEIGHT);Graphics2D graphics2d = (Graphics2D) graphics;drawRandomNum(graphics2d, request);return image;
}
@RequestMapping(value = { "/upload" }, method = RequestMethod.POST)
public final String upload(@Multipart MultipartFile uploadFile) throws IOException {String upload = "D:/www.yhj.com/webapps/upload/";String path = upload + uploadFile.getFileName();File file = new File(path);uploadFile.save(file);return "/upload/" + uploadFile.getFileName();
}@POST({"/upload/multi"})
public final String multiUpload(HttpServletResponse response, @Multipart MultipartFile[] files) throws IOException {String upload = "D:/www.yhj.com/webapps/upload/";for (MultipartFile multipartFile : files) {String path = upload + multipartFile.getFileName();File file = new File(path);System.out.println(path);if (!multipartFile.save(file)) {return "<script>alert('upload error !')</script>";//response.getWriter().print("<script>alert('upload error !')</script>");}}//response.getWriter().print("<script>alert('upload success !')</script>");return "<script>alert('upload success !')</script>";
}
@Controller
public class UserController {/* <controller prefix="/WEB-INF/view/" suffix=".ftl"><action resource="login" name="login" /><action resource="register" name="register" /></controller> */// @GET("login")@RequestMapping(value = "/login" , method = RequestMethod.GET)public String login() {return "/login/login";//支持jsp,FreeMarker,Thymeleaf,自定義視圖}// @POST("login")@ResponseBody@RequestMapping(value = "/login" , method = RequestMethod.POST)public String login(@RequestParam(required = true) String userId, @RequestParam(required = true) String passwd) {// service...if(userId.equals(passwd)) {return "{"msg":"登錄成功"}";}return "{"msg":"登錄失敗"}";//支持pojo轉(zhuǎn)json}
}
開源 請查看
TAKETODAY/today-web?github.com
總結(jié)
以上是生活随笔 為你收集整理的从零开始写javaweb框架 pdf_大学写的一个 Java Web 框架 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。