QuickStart ###通过Seed-MVC快速创建项目 首先,导入jar包 Maven: com.opdar.seed:seed-aop:0.1.0 Maven: com.opdar.seed:seed-base:0.1.0 Maven: com.opdar.seed:seed-database:0.1.0 Maven: com.opdar.seed:seed-mvc:0.1.0 Maven: com.opdar.seed:seed-mvc-support:0.1.0 或者可以通过git clone下Seed项目,在根目录下运行mvn install命令 OSChina Git : https://git.oschina.net/opdar/Seed 当正常运行完成后,在pom下载入seed-mvc-support即可 ``` <dependencies> <dependency> <groupId>com.opdar.seed</groupId> <artifactId>seed-mvc-support</artifactId> <version>0.1.0</version> </dependency> </dependencies> ``` 首先创建配置类,如下所示 ``` public class TomcatConfig extends DefaultConfig { Properties properties = new Properties(); public TomcatConfig() { load("/configures/config.properties"); } public void load(String propertiesFile) { try { properties.load(TomcatConfig.class.getResourceAsStream(propertiesFile)); setProperties(properties); } catch (Exception e) { e.printStackTrace(); } } @Override public void onCreate() { System.out.println("onCreate"); } @Override public void onDestory() { System.out.println("onDestory"); } } ``` 我们看到,这里载入了一个config.properties,内容如下: ``` #控制器目录 com.opdar.seed.controllers = com.opdar.seed.quickstart.controllers #模板渲染页面目录 com.opdar.seed.web.pages = com.opdar.pages #静态资源文件目录 com.opdar.seed.web.public = /public,classpath:com.opdar.public;/ext,WEB-INF/ext; #默认目录加载页 com.opdar.seed.web.default.pages = index.html;index.sed; ``` com.opdar.seed.controllers为控制器的包路径,SeedMVC会自动扫描该包下的控制器,并生成路由。 com.opdar.seed.web.pages为一些页面渲染目录,默认加载的是该classpath下的配置路径。 com.opdar.seed.web.public为公共目录,即静态资源目录,可配置classpath,见例子。 com.opdar.seed.web.default.pages为默认页,分号分割。如访问了 “/”路径,默认将会跳转至/index.html路径访问或者/index.sed路径访问 好了,基础配置到此完成。 接下来来配置web.xml ``` <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <listener> <listener-class>com.opdar.framework.server.supports.servlet.ServletSupport</listener-class> </listener> <context-param> <param-name>config</param-name> <param-value>com.opdar.seed.quickstart.base.TomcatConfig</param-value> </context-param> <servlet> <servlet-name>seedservlet</servlet-name> <servlet-class>com.opdar.framework.server.supports.servlet.SeedServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>seedservlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> ``` 将刚才的com.opdar.seed.quickstart.base.TomcatConfig类配置至context-param下,其余按照以上配置即可。 至此,一个简单的SeedMVC项目已经配置完成 我们创建一个Controller试试 ``` package com.opdar.seed.quickstart.controllers; import com.opdar.framework.web.anotations.Controller; import com.opdar.framework.web.anotations.Router; import com.opdar.framework.web.interfaces.View; import com.opdar.framework.web.views.DefaultView; import com.opdar.seed.quickstart.beans.TestBean; @Controller(value = "/", prefix = "html") public class QuickController { @Router("index") public View index() { return new DefaultView("Hello SeedMVC!"); } @Router("printJson") public View printJson() { TestBean bean = new TestBean(); return new DefaultView(bean); } } ``` 可以运行项目,访问http://xxx/或者http://xxx/printJson.html试试! ###快速集成Database 我们继续在上个项目中集成数据库试试,使用Seed-Database集成数据库功能非常的简单,可以通过jdbcurl快速集成。我们在config.properties加入以下配置: ``` #数据库配置 #是否开启ORM功能 0为关闭 1为开启 com.opdar.seed.activerecord = 1 #是否开启JDBCURL模式 com.opdar.seed.sql.openurl = 1 #jdbc地址 com.opdar.seed.sql.jdbcUrl = jdbc:mysql://localhost:3306/idoc?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull #驱动 com.opdar.seed.sql.driver = com.mysql.jdbc.Driver #用户名 com.opdar.seed.sql.username = root #密码 com.opdar.seed.sql.password = 123456 ``` 然后在代码中就可以通过 ··· BaseDatabase database = Context.get(BaseDatabase.class); ··· 获取到database对象,database对象的操作后续会继续讲到。 或者可以在Controller的代码中这样写: ``` @Controller(value = "/idoc", prefix = "html") public class IDocController { @Inject IDatabase database; @Router("find") public View find() { IDao<IDocEntity> idocDao = database.getDao(IDocEntity.class); Map<String, Object> dataModels = new HashMap<String, Object>(); dataModels.put("interfaces",idocDao.SELECT().END().findAll()); return new TemplateView("index.html",dataModels); } } ``` 也可以获取到该对象。 ###快速集成Seed-Template Seed自带模板功能,载入相关包后,只要返回TemplateView类型的视图,即可以使用模板功能。 TemplateView(文件路径,数据) 文件路径为ClassPath路径,如ClassPath的根节点下有index.html文件,即可这样写: ``` return new TemplateView("index.html",dataModels); ``` ###默认视图 默认视图有 DefaultView 如果是基础数据类型和String类型,则会返回原始值,如返回值为对象,则会返回JSON数据格式。 ErrorView 错误响应视图 JSONView 返回JSON数据格式,被DefaultView所调用 FileView 返回一个文件 HtmlView 返回一个Html RedirectView 跳转 ###自定义视图 说完Seed-Template与默认视图的同时,我们也可以自定义视图,来使用其他的模板引擎,如Freemarker、Velocity、Beetl,以Beetl为例。 ``` public class SystemBeetlView implements View { private static com.ihygeia.background.module.plan.plugin.GroupTemplate gt; String contentType = "text/html"; public static GroupTemplate getGroupTemplate() { if (gt == null) { ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(); Configuration cfg = null; try { cfg = Configuration.defaultConfiguration(); gt = new GroupTemplate(resourceLoader, cfg); Map<String, Object> vars = new HashMap<String, Object>(); gt.setClassLoader(SystemBeetlView.class.getClassLoader()); gt.setSharedVars(vars); } catch (IOException e) { e.printStackTrace(); } } return gt; } String templatePath = null; Map<String, ?> dataModels = null; public SystemBeetlView(String templatePath, Map<String, ?> dataModels) { this.templatePath = templatePath; this.dataModels = dataModels; } public void setContentType(String contentType) { this.contentType = contentType; } public Map<String, String> headers() { return null; } public byte[] renderView() { Template t = getGroupTemplate().getTemplate(templatePath); if (dataModels != null) t.binding(dataModels); try { return t.render().getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return new byte[0]; } public String contentType() { return contentType; } public int getCode() { return 200; } } ``` 这就是一个Beetl模板引擎的视图了,接下来可以在路由中这样调用。 ``` return new SystemBeetlView("index.html",dataModels); ```