├── README.md ├── restjs-spring-mvc ├── .classpath ├── .project ├── .settings │ ├── .jsdtscope │ ├── org.eclipse.jdt.core.prefs │ ├── org.eclipse.wst.common.component │ ├── org.eclipse.wst.common.project.facet.core.xml │ ├── org.eclipse.wst.jsdt.ui.superType.container │ └── org.eclipse.wst.jsdt.ui.superType.name └── WebContent │ ├── META-INF │ └── MANIFEST.MF │ ├── WEB-INF │ ├── springmvc-servlet.xml │ ├── src │ │ └── com │ │ │ └── hmkcode │ │ │ ├── controllers │ │ │ └── MyController.java │ │ │ └── vo │ │ │ └── Person.java │ └── web.xml │ ├── index.html │ └── restjs │ ├── app.html │ └── scripts │ └── main.js ├── spring-@profile ├── .classpath ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── hmkcode │ ├── App.java │ ├── spring │ ├── SpringConfigDev.java │ └── SpringConfigPro.java │ └── vo │ └── Person.java ├── spring-core-config ├── .classpath ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── hmkcode │ │ ├── App.java │ │ └── spring │ │ ├── beans │ │ ├── AnotherBean.java │ │ └── MyBean.java │ │ └── config │ │ └── JavaConfig.java │ └── resources │ └── config │ ├── XMLConfig-Annotation.xml │ └── XMLConfig.xml ├── spring-custom-event ├── .classpath ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── hmkcode │ │ ├── App.java │ │ ├── beans │ │ ├── Login.java │ │ └── LoginTracker.java │ │ └── event │ │ └── LoginEvent.java │ └── resources │ └── spring-config.xml ├── spring-exception-controlleradvice ├── .classpath ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── hmkcode │ ├── App.java │ ├── controllers │ └── Controller.java │ └── exception │ └── ControllerAdvisor.java ├── spring-mvc-excel-view ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── hmkcode │ │ ├── controllers │ │ └── Controller.java │ │ ├── model │ │ └── Link.java │ │ └── view │ │ └── ExcelXlsxView.java │ └── webapp │ ├── WEB-INF │ ├── rest-servlet.xml │ ├── views.xml │ └── web.xml │ └── index.html ├── spring-mvc-file-upload ├── .classpath └── WebContent │ ├── WEB-INF │ ├── rest-servlet.xml │ ├── src │ │ └── com │ │ │ └── hmkcode │ │ │ ├── controllers │ │ │ └── RestController.java │ │ │ └── vo │ │ │ └── UploadedFile.java │ └── web.xml │ └── index.html ├── spring-mvc-jquery-file-upload ├── .classpath ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── hmkcode │ │ └── spring │ │ └── mvc │ │ ├── controllers │ │ └── FileController.java │ │ └── model │ │ └── FileMeta.java │ └── webapp │ ├── WEB-INF │ ├── rest-servlet.xml │ └── web.xml │ ├── bootstrap │ ├── css │ │ └── bootstrap.css │ ├── img │ │ ├── glyphicons-halflings-white.png │ │ └── glyphicons-halflings.png │ └── js │ │ └── bootstrap.min.js │ ├── css │ └── dropzone.css │ ├── index.html │ └── js │ ├── jquery.1.9.1.min.js │ ├── jquery.fileupload.js │ ├── jquery.iframe-transport.js │ ├── myuploadfunction.js │ └── vendor │ └── jquery.ui.widget.js ├── spring-mvc-json-pdf-xls-excel ├── .classpath ├── articles.xml ├── get.pdf ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── hmkcode │ │ ├── controllers │ │ └── MyController.java │ │ ├── view │ │ ├── ExcelView.java │ │ ├── PDFView.java │ │ └── abstractview │ │ │ ├── AbstractExcelView.java │ │ │ └── AbstractPdfView.java │ │ └── vo │ │ └── Article.java │ └── webapp │ ├── WEB-INF │ ├── rest-servlet.xml │ └── web.xml │ ├── bootstrap │ └── css │ │ └── bootstrap.min.css │ └── index.html ├── spring-mvc-json ├── .classpath ├── .project ├── README.md ├── WebContent │ ├── META-INF │ │ └── MANIFEST.MF │ ├── WEB-INF │ │ ├── classes │ │ │ └── com │ │ │ │ └── hmkcode │ │ │ │ ├── controllers │ │ │ │ └── RestController.class │ │ │ │ └── vo │ │ │ │ └── Person.class │ │ ├── rest-servlet.xml │ │ ├── src │ │ │ └── com │ │ │ │ └── hmkcode │ │ │ │ ├── controllers │ │ │ │ └── RestController.java │ │ │ │ └── vo │ │ │ │ └── Person.java │ │ └── web.xml │ └── index.html └── pom.xml ├── spring-mvc-root-webapplicationcontext ├── .classpath ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hmkcode │ │ │ └── spring │ │ │ ├── beans │ │ │ └── RootBean.java │ │ │ └── controllers │ │ │ └── MyController.java │ └── webapp │ │ ├── WEB-INF │ │ ├── rest-servlet.xml │ │ ├── root-context.xml │ │ └── web.xml │ │ └── index.html │ └── test │ └── java │ └── com │ └── hmkcode │ └── spring │ └── AppTest.java ├── spring-mvc-security ├── .classpath ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── hmkcode │ │ └── controllers │ │ └── RestController.java │ └── webapp │ ├── WEB-INF │ ├── rest-servlet.xml │ ├── spring-security.xml │ └── web.xml │ └── hello.jsp ├── spring-mvc-test ├── .classpath ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hmkcode │ │ │ ├── controllers │ │ │ └── Controller.java │ │ │ └── model │ │ │ └── Link.java │ └── webapp │ │ ├── WEB-INF │ │ ├── rest-servlet.xml │ │ └── web.xml │ │ └── index.html │ └── test │ ├── java │ └── com │ │ └── hmkcode │ │ └── test │ │ └── TestController.java │ └── resources │ └── rest-servlet.xml ├── spring-mybatis-junit ├── .classpath ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hmkcode │ │ │ └── spring │ │ │ └── mybatis │ │ │ ├── App.java │ │ │ ├── mapper │ │ │ └── Mapper.java │ │ │ ├── service │ │ │ └── Service.java │ │ │ └── vo │ │ │ ├── Child.java │ │ │ └── Parent.java │ └── resources │ │ ├── com │ │ └── hmkcode │ │ │ └── spring │ │ │ └── mybatis │ │ │ ├── mapper │ │ │ └── Mapper.xml │ │ │ └── mybatis-config.xml │ │ ├── schema.sql │ │ ├── spring-config.xml │ │ └── test-data.sql │ └── test │ ├── java │ └── com │ │ └── hmkcode │ │ └── spring │ │ └── mybatis │ │ └── AppTest.java │ └── resources │ └── spring-config-test.xml ├── spring-mybatis ├── .classpath ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hmkcode │ │ │ └── spring │ │ │ └── mybatis │ │ │ ├── App.java │ │ │ ├── mapper │ │ │ └── Mapper.java │ │ │ ├── service │ │ │ └── Service.java │ │ │ └── vo │ │ │ └── Person.java │ └── resources │ │ ├── com │ │ └── hmkcode │ │ │ └── spring │ │ │ └── mybatis │ │ │ └── mapper │ │ │ └── Mapper.xml │ │ ├── schema.sql │ │ ├── spring-config.xml │ │ └── test-data.sql │ └── test │ └── java │ └── com │ └── hmkcode │ └── spring │ └── mybatis │ └── AppTest.java └── spring-profile ├── .classpath ├── pom.xml └── src └── main ├── java └── com │ └── hmkcode │ ├── App.java │ ├── spring │ └── SpringConfig.java │ └── vo │ └── Person.java └── resources ├── spring-config-all.xml ├── spring-config-development.xml └── spring-config-production.xml /README.md: -------------------------------------------------------------------------------- 1 | Spring-Framework 2 | ================ 3 | 4 | spring framework related code 5 | 6 | restjs-spring-mvc http://hmkcode.com/spring-mvc-rest-js/ 7 | 8 | spring-mvc-jquery-file-upload http://hmkcode.com/spring-mvc-jquery-file-upload-multiple-dragdrop-progress/ 9 | 10 | spring-mybatis-junit http://hmkcode.com/mybatis-spring-junit-the-missing-part/ 11 | 12 | spring-profile http://hmkcode.com/spring-profile 13 | -------------------------------------------------------------------------------- /restjs-spring-mvc/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /restjs-spring-mvc/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | restjs-spring-mvc 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.wst.jsdt.core.javascriptValidator 10 | 11 | 12 | 13 | 14 | org.eclipse.jdt.core.javabuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.wst.common.project.facet.core.builder 20 | 21 | 22 | 23 | 24 | org.eclipse.wst.validation.validationbuilder 25 | 26 | 27 | 28 | 29 | 30 | org.eclipse.jem.workbench.JavaEMFNature 31 | org.eclipse.wst.common.modulecore.ModuleCoreNature 32 | org.eclipse.wst.common.project.facet.core.nature 33 | org.eclipse.jdt.core.javanature 34 | org.eclipse.wst.jsdt.core.jsNature 35 | 36 | 37 | -------------------------------------------------------------------------------- /restjs-spring-mvc/.settings/.jsdtscope: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /restjs-spring-mvc/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Mon Feb 18 19:57:10 AST 2013 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 5 | org.eclipse.jdt.core.compiler.compliance=1.6 6 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 7 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 8 | org.eclipse.jdt.core.compiler.source=1.6 9 | -------------------------------------------------------------------------------- /restjs-spring-mvc/.settings/org.eclipse.wst.common.component: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /restjs-spring-mvc/.settings/org.eclipse.wst.common.project.facet.core.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /restjs-spring-mvc/.settings/org.eclipse.wst.jsdt.ui.superType.container: -------------------------------------------------------------------------------- 1 | org.eclipse.wst.jsdt.launching.baseBrowserLibrary -------------------------------------------------------------------------------- /restjs-spring-mvc/.settings/org.eclipse.wst.jsdt.ui.superType.name: -------------------------------------------------------------------------------- 1 | Window -------------------------------------------------------------------------------- /restjs-spring-mvc/WebContent/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: 3 | 4 | -------------------------------------------------------------------------------- /restjs-spring-mvc/WebContent/WEB-INF/springmvc-servlet.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /restjs-spring-mvc/WebContent/WEB-INF/src/com/hmkcode/controllers/MyController.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.controllers; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.Model; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.ResponseBody; 7 | 8 | import com.hmkcode.vo.Person; 9 | 10 | @Controller 11 | public class MyController { 12 | 13 | @RequestMapping("/view") 14 | public @ResponseBody Person getPerson(){ 15 | Person person = new Person(); 16 | person.setId(1); 17 | person.setName("hmk"); 18 | return person; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /restjs-spring-mvc/WebContent/WEB-INF/src/com/hmkcode/vo/Person.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.vo; 2 | 3 | 4 | public class Person { 5 | 6 | private int id; 7 | private String name; 8 | public int getId() { 9 | return id; 10 | } 11 | public void setId(int id) { 12 | this.id = id; 13 | } 14 | public String getName() { 15 | return name; 16 | } 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | public String toString(){ 21 | return "ID: "+id+" - Name: "+name; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /restjs-spring-mvc/WebContent/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | springmvc 5 | org.springframework.web.servlet.DispatcherServlet 6 | 1 7 | 8 | 9 | springmvc 10 | /rest/* 11 | 12 | 13 | index.html 14 | 15 | -------------------------------------------------------------------------------- /restjs-spring-mvc/WebContent/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Spring MVC 6 | 7 | 8 | Hello! 9 | 10 | -------------------------------------------------------------------------------- /restjs-spring-mvc/WebContent/restjs/app.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Spring MVC + REST.js Sample Page 5 | 6 | 7 | 8 | 9 |

Spring MVC + REST.js

10 |

11 | 12 |
13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /restjs-spring-mvc/WebContent/restjs/scripts/main.js: -------------------------------------------------------------------------------- 1 | require(["rest/rest","rest/interceptor/mime","jquery"], function(rest,mime,$) { 2 | 3 | /*rest({ path: 'http://localhost:8080/restjs-spring-mvc/rest/view' }).then(function(response) { 4 | console.log('response: ', response); 5 | alert("asd"); 6 | });*/ 7 | 8 | client = rest.chain(mime); 9 | client({ path: 'http://localhost:8080/restjs-spring-mvc/rest/view' }).then(function(response) { 10 | console.log('response: ', response); 11 | $(function() { 12 | $('#person-id').text(response.entity.id); 13 | $('#person-name').text(response.entity.name); 14 | }); 15 | 16 | 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /spring-@profile/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring-@profile/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.hmkcode 6 | spring-profile 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | spring-profile 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 3.1.0.RELEASE 16 | 17 | 18 | 19 | 20 | junit 21 | junit 22 | 4.11 23 | test 24 | 25 | 26 | org.springframework 27 | spring-core 28 | ${org.springframework.version} 29 | 30 | 31 | org.springframework 32 | spring-beans 33 | ${org.springframework.version} 34 | 35 | 36 | org.springframework 37 | spring-context 38 | ${org.springframework.version} 39 | 40 | 41 | org.springframework 42 | spring-context-support 43 | ${org.springframework.version} 44 | 45 | 46 | cglib 47 | cglib-nodep 48 | 2.2 49 | compile 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /spring-@profile/src/main/java/com/hmkcode/App.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode; 2 | 3 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 | 5 | 6 | import com.hmkcode.vo.Person; 7 | 8 | public class App 9 | { 10 | public static void main( String[] args ) 11 | { 12 | 13 | AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); 14 | ctx.getEnvironment().setActiveProfiles("development"); //or production 15 | ctx.scan("com.hmkcode.spring"); // register all @Configuration classes 16 | //ctx.register(SpringConfigDev.class); 17 | ctx.refresh(); 18 | Person person = (Person)ctx.getBean("person"); 19 | 20 | 21 | System.out.println("Person: "+person); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-@profile/src/main/java/com/hmkcode/spring/SpringConfigDev.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.Profile; 6 | 7 | import com.hmkcode.vo.Person; 8 | 9 | @Configuration 10 | @Profile("development") 11 | public class SpringConfigDev { 12 | 13 | @Bean 14 | public Person person(){ 15 | 16 | Person person = new Person(); 17 | person.setId(1); 18 | person.setName("dev-person"); 19 | 20 | return person; 21 | } 22 | } -------------------------------------------------------------------------------- /spring-@profile/src/main/java/com/hmkcode/spring/SpringConfigPro.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.Profile; 6 | 7 | import com.hmkcode.vo.Person; 8 | 9 | @Configuration 10 | @Profile("production") 11 | public class SpringConfigPro { 12 | 13 | @Bean 14 | public Person person(){ 15 | 16 | Person person = new Person(); 17 | person.setId(2); 18 | person.setName("pro-person"); 19 | 20 | return person; 21 | } 22 | } -------------------------------------------------------------------------------- /spring-@profile/src/main/java/com/hmkcode/vo/Person.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.vo; 2 | 3 | public class Person { 4 | 5 | private int id; 6 | private String name; 7 | public int getId() { 8 | return id; 9 | } 10 | public void setId(int id) { 11 | this.id = id; 12 | } 13 | public String getName() { 14 | return name; 15 | } 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | 20 | public String toString(){ 21 | return "Id: "+id+" - Name: "+name; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-core-config/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | <<<<<<< HEAD 15 | 16 | 17 | 18 | 19 | 20 | ======= 21 | 22 | 23 | 24 | 25 | 26 | >>>>>>> updated 27 | -------------------------------------------------------------------------------- /spring-core-config/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.hmkcode 6 | spring-core-container 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | spring-core-container 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 3.1.0.RELEASE 16 | 17 | 18 | 19 | 20 | org.springframework 21 | spring-core 22 | ${org.springframework.version} 23 | 24 | 25 | org.springframework 26 | spring-beans 27 | ${org.springframework.version} 28 | 29 | 30 | org.springframework 31 | spring-context 32 | ${org.springframework.version} 33 | 34 | 35 | cglib 36 | cglib 37 | 2.2.2 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /spring-core-config/src/main/java/com/hmkcode/App.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | import org.springframework.context.support.ClassPathXmlApplicationContext; 6 | 7 | import com.hmkcode.spring.beans.AnotherBean; 8 | import com.hmkcode.spring.beans.MyBean; 9 | import com.hmkcode.spring.config.JavaConfig; 10 | 11 | 12 | public class App 13 | { 14 | public static void main( String[] args ) 15 | { 16 | 17 | 18 | ApplicationContext ctxXML = new ClassPathXmlApplicationContext("config/XMLConfig.xml"); 19 | AnotherBean anotherBean = (AnotherBean) ctxXML.getBean("anotherBean"); 20 | 21 | System.out.println( anotherBean); 22 | 23 | //--------------------------------------- 24 | 25 | 26 | ApplicationContext ctxAnnotation = new ClassPathXmlApplicationContext("config/XMLConfig-Annotation.xml"); 27 | anotherBean = (AnotherBean) ctxAnnotation.getBean("anotherBean"); 28 | 29 | System.out.println( anotherBean); 30 | 31 | //--------------------------------------- 32 | 33 | 34 | ApplicationContext ctxJavaConfig = new AnnotationConfigApplicationContext(JavaConfig.class); 35 | anotherBean = (AnotherBean) ctxJavaConfig.getBean("anotherBean2"); 36 | 37 | System.out.println( anotherBean); 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-core-config/src/main/java/com/hmkcode/spring/beans/AnotherBean.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.beans; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Qualifier; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component(value="anotherBean") 8 | public class AnotherBean { 9 | 10 | private MyBean myBean; 11 | 12 | public MyBean getMyBean() { 13 | return myBean; 14 | } 15 | 16 | @Autowired 17 | @Qualifier("myBean") 18 | public void setMyBean(MyBean myBean) { 19 | this.myBean = myBean; 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return "AnotherBean [myBean=" + myBean + "]"; 25 | } 26 | 27 | 28 | } -------------------------------------------------------------------------------- /spring-core-config/src/main/java/com/hmkcode/spring/beans/MyBean.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.beans; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Component(value="myBean") 7 | public class MyBean { 8 | 9 | private String name; 10 | 11 | public String getName() { 12 | return name; 13 | } 14 | 15 | @Value(value="AnnotationBean") 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return "MyBean [name=" + name + "]"; 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /spring-core-config/src/main/java/com/hmkcode/spring/config/JavaConfig.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import com.hmkcode.spring.beans.AnotherBean; 6 | import com.hmkcode.spring.beans.MyBean; 7 | 8 | @Configuration 9 | public class JavaConfig { 10 | 11 | @Bean 12 | public MyBean myBean(){ 13 | return new MyBean(); 14 | } 15 | 16 | @Bean(name = "anotherBean2") 17 | public AnotherBean anotherBean(){ 18 | return new AnotherBean(); 19 | } 20 | } -------------------------------------------------------------------------------- /spring-core-config/src/main/resources/config/XMLConfig-Annotation.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /spring-core-config/src/main/resources/config/XMLConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-custom-event/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /spring-custom-event/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.hmkcode 6 | spring-custom-event 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | spring-custom-event 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 3.1.0.RELEASE 16 | 17 | 18 | 19 | 20 | org.springframework 21 | spring-core 22 | ${org.springframework.version} 23 | 24 | 25 | org.springframework 26 | spring-beans 27 | ${org.springframework.version} 28 | 29 | 30 | org.springframework 31 | spring-context 32 | ${org.springframework.version} 33 | 34 | 35 | org.springframework 36 | spring-context-support 37 | ${org.springframework.version} 38 | 39 | 40 | 41 | 42 | 43 | org.codehaus.mojo 44 | exec-maven-plugin 45 | 1.2.1 46 | 47 | 48 | my-execution 49 | compile 50 | 51 | java 52 | 53 | 54 | 55 | 56 | com.hmkcode.App 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /spring-custom-event/src/main/java/com/hmkcode/App.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode; 2 | 3 | import org.springframework.context.support.ClassPathXmlApplicationContext; 4 | 5 | import com.hmkcode.beans.Login; 6 | 7 | 8 | public class App 9 | { 10 | public static void main( String[] args ) 11 | { 12 | ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml"); 13 | 14 | Login login = (Login) ctx.getBean("login"); 15 | login.login(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-custom-event/src/main/java/com/hmkcode/beans/Login.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.beans; 2 | 3 | import org.springframework.context.ApplicationEventPublisher; 4 | import org.springframework.context.ApplicationEventPublisherAware; 5 | 6 | import com.hmkcode.event.LoginEvent; 7 | 8 | public class Login implements ApplicationEventPublisherAware { 9 | 10 | private String username; 11 | private ApplicationEventPublisher publisher; 12 | 13 | @Override 14 | public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { 15 | this.publisher = publisher; 16 | } 17 | 18 | public void login(){ 19 | 20 | //do the login.. 21 | 22 | //fire the event 23 | LoginEvent event = new LoginEvent(this); 24 | publisher.publishEvent(event); 25 | } 26 | 27 | public String getUsername() { 28 | return username; 29 | } 30 | 31 | public void setUsername(String username) { 32 | this.username = username; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /spring-custom-event/src/main/java/com/hmkcode/beans/LoginTracker.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.beans; 2 | 3 | import org.springframework.context.ApplicationListener; 4 | import com.hmkcode.event.LoginEvent; 5 | 6 | public class LoginTracker implements ApplicationListener { 7 | 8 | @Override 9 | public void onApplicationEvent(LoginEvent event) { 10 | 11 | System.out.println("\n"+((Login)event.getSource()).getUsername()+" logged-in @ "+event.getDate()+"\n"); 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-custom-event/src/main/java/com/hmkcode/event/LoginEvent.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.event; 2 | 3 | import java.util.Date; 4 | 5 | import org.springframework.context.ApplicationEvent; 6 | 7 | public class LoginEvent extends ApplicationEvent { 8 | 9 | 10 | private Date date; 11 | 12 | public LoginEvent(Object source){ 13 | super(source); 14 | this.date = new Date(getTimestamp()); 15 | } 16 | 17 | public Date getDate() { 18 | return date; 19 | } 20 | 21 | public void setDate(Date date) { 22 | this.date = date; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spring-custom-event/src/main/resources/spring-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /spring-exception-controlleradvice/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /spring-exception-controlleradvice/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.hmkcode 6 | spring-exception-controlleradvice 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | spring-boot 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 1.8 16 | 1.8 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 2.4.2 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | org.codehaus.mojo 37 | exec-maven-plugin 38 | 3.0.0 39 | 40 | com.hmkcode.App 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /spring-exception-controlleradvice/src/main/java/com/hmkcode/App.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode; 2 | 3 | import org.springframework.boot.*; 4 | import org.springframework.boot.autoconfigure.*; 5 | import org.springframework.stereotype.*; 6 | 7 | @Controller 8 | @SpringBootApplication 9 | public class App { 10 | 11 | 12 | public static void main(String[] args) throws Exception { 13 | SpringApplication.run(App.class, args); 14 | } 15 | } -------------------------------------------------------------------------------- /spring-exception-controlleradvice/src/main/java/com/hmkcode/controllers/Controller.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.controllers; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.ResponseBody; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | 8 | @RestController 9 | public class Controller { 10 | 11 | 12 | @RequestMapping("/ok") 13 | public @ResponseBody String ok() throws Exception { 14 | return "OK"; 15 | } 16 | 17 | @RequestMapping("/exception") 18 | public @ResponseBody String exception() throws Exception { 19 | throw new Exception("Error"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /spring-exception-controlleradvice/src/main/java/com/hmkcode/exception/ControllerAdvisor.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.exception; 2 | 3 | 4 | import javax.servlet.http.HttpServletRequest; 5 | 6 | import org.springframework.web.bind.annotation.ControllerAdvice; 7 | import org.springframework.web.bind.annotation.ExceptionHandler; 8 | import org.springframework.web.bind.annotation.ResponseBody; 9 | import org.springframework.web.bind.annotation.ResponseStatus; 10 | import org.springframework.http.HttpStatus; 11 | 12 | @ControllerAdvice 13 | public class ControllerAdvisor { 14 | 15 | 16 | @ExceptionHandler(Exception.class) 17 | @ResponseStatus(HttpStatus.BAD_REQUEST) 18 | 19 | public @ResponseBody String generalException(final Exception exception, 20 | final HttpServletRequest request) { 21 | 22 | return exception.getMessage()+" while calling: "+request.getRequestURI(); 23 | 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /spring-mvc-excel-view/README.md: -------------------------------------------------------------------------------- 1 | Spring MVC | Excel View Resolver 2 | ================================ 3 | 4 | 5 | Spring MVC can render retrieved data into views of different format such as PDF, JSP or Excel. Spring framework view resolvers e.g. `InternalResourceViewResolver`, `XmlViewResolver`, `ResourceBundleViewResolver` and a few others enable you to render model into views. This post shows you how to render model into Excel view using XmlViewResolver. 6 | -------------------------------------------------------------------------------- /spring-mvc-excel-view/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.hmkcode 6 | spring-mvc-excel-view 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | spring-mvc-excel-view 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 4.3.14.RELEASE 16 | 3.17 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.springframework 25 | spring-context 26 | ${spring.version} 27 | 28 | 29 | 30 | org.springframework 31 | spring-core 32 | ${spring.version} 33 | 34 | 35 | 36 | org.springframework 37 | spring-beans 38 | ${spring.version} 39 | 40 | 41 | 42 | org.springframework 43 | spring-web 44 | ${spring.version} 45 | 46 | 47 | 48 | org.springframework 49 | spring-webmvc 50 | ${spring.version} 51 | 52 | 53 | 54 | org.springframework 55 | spring-expression 56 | ${spring.version} 57 | 58 | 59 | 60 | 61 | commons-logging 62 | commons-logging 63 | 1.2 64 | 65 | 66 | 67 | 68 | org.apache.poi 69 | poi 70 | ${poi.version} 71 | 72 | 73 | org.apache.poi 74 | poi-ooxml 75 | ${poi.version} 76 | 77 | 78 | 79 | javax.servlet 80 | javax.servlet-api 81 | 4.0.0 82 | provided 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | org.eclipse.jetty 92 | jetty-maven-plugin 93 | 9.4.8.v20171121 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /spring-mvc-excel-view/src/main/java/com/hmkcode/controllers/Controller.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.controllers; 2 | 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | import org.springframework.web.bind.annotation.RestController; 9 | import org.springframework.web.servlet.ModelAndView; 10 | 11 | import com.hmkcode.model.Link; 12 | 13 | @RestController 14 | public class Controller { 15 | 16 | @RequestMapping(value = "/viewExcel", method = RequestMethod.GET) 17 | public ModelAndView getExcel(){ 18 | System.out.println("getExcel!"); 19 | 20 | List links = new LinkedList(); 21 | links.add(new Link("Android", "android.com")); 22 | links.add(new Link("Spring", "spring.io")); 23 | links.add(new Link("Firebase", "firebase.com")); 24 | 25 | return new ModelAndView("ExcelXlsxView", "model", links); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring-mvc-excel-view/src/main/java/com/hmkcode/model/Link.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.model; 2 | 3 | public class Link { 4 | 5 | private String title; 6 | private String url; 7 | 8 | public Link(String title, String url){ 9 | this.title = title; 10 | this.url = url; 11 | } 12 | public String getTitle() { 13 | return title; 14 | } 15 | public void setTitle(String title) { 16 | this.title = title; 17 | } 18 | public String getUrl() { 19 | return url; 20 | } 21 | public void setUrl(String url) { 22 | this.url = url; 23 | } 24 | @Override 25 | public String toString() { 26 | return "Link [title=" + title + ", url=" + url + "]"; 27 | } 28 | 29 | 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /spring-mvc-excel-view/src/main/java/com/hmkcode/view/ExcelXlsxView.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.view; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.apache.poi.ss.usermodel.Row; 10 | import org.apache.poi.ss.usermodel.Sheet; 11 | import org.apache.poi.ss.usermodel.Workbook; 12 | import org.springframework.web.servlet.view.document.AbstractXlsxView; 13 | 14 | import com.hmkcode.model.Link; 15 | 16 | public class ExcelXlsxView extends AbstractXlsxView { 17 | 18 | @Override 19 | protected void buildExcelDocument(Map model, 20 | Workbook workbook, 21 | HttpServletRequest request, 22 | HttpServletResponse response) throws Exception { 23 | 24 | @SuppressWarnings("unchecked") 25 | List links = (List) model.get("model"); 26 | 27 | // create a new Excel sheet 28 | Sheet sheet = workbook.createSheet("Sheet"); 29 | 30 | int i = 0; 31 | Row row = null; 32 | for(Link link:links){ 33 | row = sheet.createRow(i++); 34 | row.createCell(0).setCellValue(link.getTitle()); 35 | row.createCell(1).setCellValue(link.getUrl()); 36 | } 37 | 38 | 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-mvc-excel-view/src/main/webapp/WEB-INF/rest-servlet.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /spring-mvc-excel-view/src/main/webapp/WEB-INF/views.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /spring-mvc-excel-view/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | SPRING MVC 8 | 9 | index.html 10 | 11 | 12 | 13 | 14 | rest 15 | org.springframework.web.servlet.DispatcherServlet 16 | 17 | 18 | rest 19 | /rest/* 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-mvc-excel-view/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Excel View Resolver 6 | 7 | 8 |

Spring MVC | Excel View Resolver

9 | View Excel 10 | 11 | -------------------------------------------------------------------------------- /spring-mvc-file-upload/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /spring-mvc-file-upload/WebContent/WEB-INF/rest-servlet.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /spring-mvc-file-upload/WebContent/WEB-INF/src/com/hmkcode/controllers/RestController.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.controllers; 2 | 3 | 4 | import java.io.IOException; 5 | import java.util.Calendar; 6 | import java.util.Iterator; 7 | 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | import org.springframework.stereotype.Controller; 11 | import org.springframework.util.FileCopyUtils; 12 | import org.springframework.web.bind.annotation.PathVariable; 13 | import org.springframework.web.bind.annotation.RequestMapping; 14 | import org.springframework.web.bind.annotation.RequestMethod; 15 | import org.springframework.web.bind.annotation.ResponseBody; 16 | import org.springframework.web.multipart.MultipartFile; 17 | import org.springframework.web.multipart.MultipartHttpServletRequest; 18 | import org.springframework.util.FileCopyUtils; 19 | 20 | import com.hmkcode.vo.UploadedFile; 21 | 22 | 23 | @Controller 24 | @RequestMapping("/cont") 25 | public class RestController { 26 | 27 | UploadedFile ufile; 28 | public RestController(){ 29 | System.out.println("init RestController"); 30 | ufile = new UploadedFile(); 31 | } 32 | 33 | 34 | @RequestMapping(value = "/get/{value}", method = RequestMethod.GET) 35 | public void get(HttpServletResponse response,@PathVariable String value){ 36 | try { 37 | 38 | response.setContentType(ufile.type); 39 | response.setContentLength(ufile.length); 40 | FileCopyUtils.copy(ufile.bytes, response.getOutputStream()); 41 | 42 | } catch (IOException e) { 43 | // TODO Auto-generated catch block 44 | e.printStackTrace(); 45 | } 46 | } 47 | 48 | 49 | @RequestMapping(value = "/upload", method = RequestMethod.POST) 50 | public @ResponseBody String upload(MultipartHttpServletRequest request, HttpServletResponse response) { 51 | 52 | //0. notice, we have used MultipartHttpServletRequest 53 | 54 | //1. get the file from the request object 55 | Iterator itr = request.getFileNames(); 56 | 57 | MultipartFile mpf = request.getFile(itr.next()); 58 | System.out.println(mpf.getOriginalFilename() +" uploaded!"); 59 | 60 | try { 61 | ufile.length = mpf.getBytes().length; 62 | ufile.bytes= mpf.getBytes(); 63 | ufile.type = mpf.getContentType(); 64 | ufile.name = mpf.getOriginalFilename(); 65 | 66 | } catch (IOException e) { 67 | // TODO Auto-generated catch block 68 | e.printStackTrace(); 69 | } 70 | 71 | //2. send it back to the client as that calls get method 72 | //we are using getTimeInMillis to avoid server cached image 73 | 74 | return ""; 75 | 76 | } 77 | 78 | } 79 | 80 | -------------------------------------------------------------------------------- /spring-mvc-file-upload/WebContent/WEB-INF/src/com/hmkcode/vo/UploadedFile.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.vo; 2 | 3 | public class UploadedFile { 4 | 5 | public int length; 6 | public byte[] bytes; 7 | public String name; 8 | public String type; 9 | } 10 | -------------------------------------------------------------------------------- /spring-mvc-file-upload/WebContent/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | SpringMVC 4 | 5 | index.html 6 | index.htm 7 | 8 | 9 | 10 | rest 11 | 12 | 13 | org.springframework.web.servlet.DispatcherServlet 14 | 15 | 16 | 17 | rest 18 | /rest/* 19 | 20 | 21 | -------------------------------------------------------------------------------- /spring-mvc-file-upload/WebContent/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Spring MVC - Upload File 6 | 7 | 8 | 9 | 47 | 48 | 49 | 50 |

SpringMVC - File Upload with/without Ajax

51 | 52 | 53 | Uploading File Without Ajax
54 |
55 | 56 | 57 |
58 | 59 | 60 |
61 |
62 | Uploading File With Ajax
63 | 64 | 65 |
66 | 67 |
68 |
69 | 70 | Using JQuery Form Plugin
71 | Using FormData Object 72 | 73 |
74 | 75 | 76 | -------------------------------------------------------------------------------- /spring-mvc-jquery-file-upload/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-mvc-jquery-file-upload/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.hmkcode 5 | spring-mvc-jquery-file-upload 6 | war 7 | 1.0-SNAPSHOT 8 | spring-mvc-jquery-file-upload Maven Webapp 9 | http://maven.apache.org 10 | 11 | 12 | 3.1.0.RELEASE 13 | 4.11 14 | 15 | 16 | 17 | 18 | org.springframework 19 | spring-core 20 | ${spring.version} 21 | 22 | 23 | org.springframework 24 | spring-web 25 | ${spring.version} 26 | 27 | 28 | org.springframework 29 | spring-webmvc 30 | ${spring.version} 31 | 32 | 33 | javax.servlet 34 | servlet-api 35 | 2.5 36 | 37 | 38 | commons-fileupload 39 | commons-fileupload 40 | 1.2 41 | 42 | 43 | commons-io 44 | commons-io 45 | 2.4 46 | 47 | 48 | org.codehaus.jackson 49 | jackson-mapper-asl 50 | 1.9.12 51 | 52 | 53 | org.codehaus.jackson 54 | jackson-core-asl 55 | 1.9.12 56 | 57 | 58 | 59 | junit 60 | junit 61 | ${junit.version} 62 | test 63 | 64 | 65 | 66 | 67 | spring-mvc-jquery-file-upload 68 | 69 | 70 | org.mortbay.jetty 71 | maven-jetty-plugin 72 | 6.1.10 73 | 74 | 10 75 | 76 | 77 | 78 | maven-compiler-plugin 79 | 80 | 1.6 81 | 1.6 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /spring-mvc-jquery-file-upload/src/main/java/com/hmkcode/spring/mvc/controllers/FileController.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mvc.controllers; 2 | 3 | import java.io.IOException; 4 | import java.util.Iterator; 5 | import java.util.LinkedList; 6 | 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.springframework.stereotype.Controller; 10 | import org.springframework.util.FileCopyUtils; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.RequestMapping; 13 | import org.springframework.web.bind.annotation.RequestMethod; 14 | import org.springframework.web.bind.annotation.ResponseBody; 15 | import org.springframework.web.multipart.MultipartFile; 16 | import org.springframework.web.multipart.MultipartHttpServletRequest; 17 | 18 | import com.hmkcode.spring.mvc.model.FileMeta; 19 | 20 | @Controller 21 | @RequestMapping("/controller") 22 | public class FileController { 23 | 24 | 25 | LinkedList files = new LinkedList(); 26 | FileMeta fileMeta = null; 27 | /*************************************************** 28 | * URL: /rest/controller/upload 29 | * upload(): receives files 30 | * @param request : MultipartHttpServletRequest auto passed 31 | * @param response : HttpServletResponse auto passed 32 | * @return LinkedList as json format 33 | ****************************************************/ 34 | @RequestMapping(value="/upload", method = RequestMethod.POST) 35 | public @ResponseBody LinkedList upload(MultipartHttpServletRequest request, HttpServletResponse response) { 36 | 37 | //1. build an iterator 38 | Iterator itr = request.getFileNames(); 39 | MultipartFile mpf = null; 40 | 41 | //2. get each file 42 | while(itr.hasNext()){ 43 | 44 | //2.1 get next MultipartFile 45 | mpf = request.getFile(itr.next()); 46 | System.out.println(mpf.getOriginalFilename() +" uploaded! "+files.size()); 47 | 48 | //2.2 if files > 10 remove the first from the list 49 | if(files.size() >= 10) 50 | files.pop(); 51 | 52 | //2.3 create new fileMeta 53 | fileMeta = new FileMeta(); 54 | fileMeta.setFileName(mpf.getOriginalFilename()); 55 | fileMeta.setFileSize(mpf.getSize()/1024+" Kb"); 56 | fileMeta.setFileType(mpf.getContentType()); 57 | 58 | try { 59 | fileMeta.setBytes(mpf.getBytes()); 60 | 61 | // copy file to local disk (make sure the path "e.g. D:/temp/files" exists) 62 | FileCopyUtils.copy(mpf.getBytes(), new FileOutputStream("D:/temp/files/"+mpf.getOriginalFilename())); 63 | 64 | } catch (IOException e) { 65 | // TODO Auto-generated catch block 66 | e.printStackTrace(); 67 | } 68 | //2.4 add to files 69 | files.add(fileMeta); 70 | 71 | } 72 | 73 | // result will be like this 74 | // [{"fileName":"app_engine-85x77.png","fileSize":"8 Kb","fileType":"image/png"},...] 75 | return files; 76 | 77 | } 78 | /*************************************************** 79 | * URL: /rest/controller/get/{value} 80 | * get(): get file as an attachment 81 | * @param response : passed by the server 82 | * @param value : value from the URL 83 | * @return void 84 | ****************************************************/ 85 | @RequestMapping(value = "/get/{value}", method = RequestMethod.GET) 86 | public void get(HttpServletResponse response,@PathVariable String value){ 87 | FileMeta getFile = files.get(Integer.parseInt(value)); 88 | try { 89 | response.setContentType(getFile.getFileType()); 90 | response.setHeader("Content-disposition", "attachment; filename=\""+getFile.getFileName()+"\""); 91 | FileCopyUtils.copy(getFile.getBytes(), response.getOutputStream()); 92 | }catch (IOException e) { 93 | // TODO Auto-generated catch block 94 | e.printStackTrace(); 95 | } 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /spring-mvc-jquery-file-upload/src/main/java/com/hmkcode/spring/mvc/model/FileMeta.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mvc.model; 2 | 3 | import org.codehaus.jackson.annotate.JsonIgnoreProperties; 4 | 5 | @JsonIgnoreProperties({"bytes"}) 6 | public class FileMeta { 7 | 8 | private String fileName; 9 | private String fileSize; 10 | private String fileType; 11 | 12 | private byte[] bytes; 13 | 14 | public String getFileName() { 15 | return fileName; 16 | } 17 | public void setFileName(String fileName) { 18 | this.fileName = fileName; 19 | } 20 | public String getFileSize() { 21 | return fileSize; 22 | } 23 | public void setFileSize(String fileSize) { 24 | this.fileSize = fileSize; 25 | } 26 | public String getFileType() { 27 | return fileType; 28 | } 29 | public void setFileType(String fileType) { 30 | this.fileType = fileType; 31 | } 32 | public byte[] getBytes() { 33 | return bytes; 34 | } 35 | public void setBytes(byte[] bytes) { 36 | this.bytes = bytes; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /spring-mvc-jquery-file-upload/src/main/webapp/WEB-INF/rest-servlet.xml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /spring-mvc-jquery-file-upload/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | Archetype Created Web Application 8 | 9 | index.html 10 | 11 | 12 | 13 | rest 14 | org.springframework.web.servlet.DispatcherServlet 15 | 16 | 17 | rest 18 | /rest/* 19 | 20 | -------------------------------------------------------------------------------- /spring-mvc-jquery-file-upload/src/main/webapp/bootstrap/img/glyphicons-halflings-white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmkcode/Spring-Framework/c13c5146f6211377b2d6fd33de91ae12036d48bd/spring-mvc-jquery-file-upload/src/main/webapp/bootstrap/img/glyphicons-halflings-white.png -------------------------------------------------------------------------------- /spring-mvc-jquery-file-upload/src/main/webapp/bootstrap/img/glyphicons-halflings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmkcode/Spring-Framework/c13c5146f6211377b2d6fd33de91ae12036d48bd/spring-mvc-jquery-file-upload/src/main/webapp/bootstrap/img/glyphicons-halflings.png -------------------------------------------------------------------------------- /spring-mvc-jquery-file-upload/src/main/webapp/css/dropzone.css: -------------------------------------------------------------------------------- 1 | #dropzone { 2 | background: #ccccc; 3 | width: 150px; 4 | height: 50px; 5 | line-height: 50px; 6 | text-align: center; 7 | font-weight: bold; 8 | } 9 | #dropzone.in { 10 | width: 600px; 11 | height: 200px; 12 | line-height: 200px; 13 | font-size: larger; 14 | } 15 | #dropzone.hover { 16 | background: lawngreen; 17 | } 18 | #dropzone.fade { 19 | -webkit-transition: all 0.3s ease-out; 20 | -moz-transition: all 0.3s ease-out; 21 | -ms-transition: all 0.3s ease-out; 22 | -o-transition: all 0.3s ease-out; 23 | transition: all 0.3s ease-out; 24 | opacity: 1; 25 | } -------------------------------------------------------------------------------- /spring-mvc-jquery-file-upload/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | jQuery File Upload Example 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 |

Spring MVC - jQuery File Upload

23 |
24 | 25 | 26 | 27 |
Drop files here
28 | 29 |
30 |
31 |
32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
File NameFile SizeFile TypeDownload
41 | 42 |
43 | 44 | 45 | -------------------------------------------------------------------------------- /spring-mvc-jquery-file-upload/src/main/webapp/js/jquery.iframe-transport.js: -------------------------------------------------------------------------------- 1 | /* 2 | * jQuery Iframe Transport Plugin 1.6.1 3 | * https://github.com/blueimp/jQuery-File-Upload 4 | * 5 | * Copyright 2011, Sebastian Tschan 6 | * https://blueimp.net 7 | * 8 | * Licensed under the MIT license: 9 | * http://www.opensource.org/licenses/MIT 10 | */ 11 | 12 | /*jslint unparam: true, nomen: true */ 13 | /*global define, window, document */ 14 | 15 | (function (factory) { 16 | 'use strict'; 17 | if (typeof define === 'function' && define.amd) { 18 | // Register as an anonymous AMD module: 19 | define(['jquery'], factory); 20 | } else { 21 | // Browser globals: 22 | factory(window.jQuery); 23 | } 24 | }(function ($) { 25 | 'use strict'; 26 | 27 | // Helper variable to create unique names for the transport iframes: 28 | var counter = 0; 29 | 30 | // The iframe transport accepts three additional options: 31 | // options.fileInput: a jQuery collection of file input fields 32 | // options.paramName: the parameter name for the file form data, 33 | // overrides the name property of the file input field(s), 34 | // can be a string or an array of strings. 35 | // options.formData: an array of objects with name and value properties, 36 | // equivalent to the return data of .serializeArray(), e.g.: 37 | // [{name: 'a', value: 1}, {name: 'b', value: 2}] 38 | $.ajaxTransport('iframe', function (options) { 39 | if (options.async) { 40 | var form, 41 | iframe, 42 | addParamChar; 43 | return { 44 | send: function (_, completeCallback) { 45 | form = $('
'); 46 | form.attr('accept-charset', options.formAcceptCharset); 47 | addParamChar = /\?/.test(options.url) ? '&' : '?'; 48 | // XDomainRequest only supports GET and POST: 49 | if (options.type === 'DELETE') { 50 | options.url = options.url + addParamChar + '_method=DELETE'; 51 | options.type = 'POST'; 52 | } else if (options.type === 'PUT') { 53 | options.url = options.url + addParamChar + '_method=PUT'; 54 | options.type = 'POST'; 55 | } else if (options.type === 'PATCH') { 56 | options.url = options.url + addParamChar + '_method=PATCH'; 57 | options.type = 'POST'; 58 | } 59 | // javascript:false as initial iframe src 60 | // prevents warning popups on HTTPS in IE6. 61 | // IE versions below IE8 cannot set the name property of 62 | // elements that have already been added to the DOM, 63 | // so we set the name along with the iframe HTML markup: 64 | iframe = $( 65 | '' 67 | ).bind('load', function () { 68 | var fileInputClones, 69 | paramNames = $.isArray(options.paramName) ? 70 | options.paramName : [options.paramName]; 71 | iframe 72 | .unbind('load') 73 | .bind('load', function () { 74 | var response; 75 | // Wrap in a try/catch block to catch exceptions thrown 76 | // when trying to access cross-domain iframe contents: 77 | try { 78 | response = iframe.contents(); 79 | // Google Chrome and Firefox do not throw an 80 | // exception when calling iframe.contents() on 81 | // cross-domain requests, so we unify the response: 82 | if (!response.length || !response[0].firstChild) { 83 | throw new Error(); 84 | } 85 | } catch (e) { 86 | response = undefined; 87 | } 88 | // The complete callback returns the 89 | // iframe content document as response object: 90 | completeCallback( 91 | 200, 92 | 'success', 93 | {'iframe': response} 94 | ); 95 | // Fix for IE endless progress bar activity bug 96 | // (happens on form submits to iframe targets): 97 | $('') 98 | .appendTo(form); 99 | form.remove(); 100 | }); 101 | form 102 | .prop('target', iframe.prop('name')) 103 | .prop('action', options.url) 104 | .prop('method', options.type); 105 | if (options.formData) { 106 | $.each(options.formData, function (index, field) { 107 | $('') 108 | .prop('name', field.name) 109 | .val(field.value) 110 | .appendTo(form); 111 | }); 112 | } 113 | if (options.fileInput && options.fileInput.length && 114 | options.type === 'POST') { 115 | fileInputClones = options.fileInput.clone(); 116 | // Insert a clone for each file input field: 117 | options.fileInput.after(function (index) { 118 | return fileInputClones[index]; 119 | }); 120 | if (options.paramName) { 121 | options.fileInput.each(function (index) { 122 | $(this).prop( 123 | 'name', 124 | paramNames[index] || options.paramName 125 | ); 126 | }); 127 | } 128 | // Appending the file input fields to the hidden form 129 | // removes them from their original location: 130 | form 131 | .append(options.fileInput) 132 | .prop('enctype', 'multipart/form-data') 133 | // enctype must be set as encoding for IE: 134 | .prop('encoding', 'multipart/form-data'); 135 | } 136 | form.submit(); 137 | // Insert the file input fields at their original location 138 | // by replacing the clones with the originals: 139 | if (fileInputClones && fileInputClones.length) { 140 | options.fileInput.each(function (index, input) { 141 | var clone = $(fileInputClones[index]); 142 | $(input).prop('name', clone.prop('name')); 143 | clone.replaceWith(input); 144 | }); 145 | } 146 | }); 147 | form.append(iframe).appendTo(document.body); 148 | }, 149 | abort: function () { 150 | if (iframe) { 151 | // javascript:false as iframe src aborts the request 152 | // and prevents warning popups on HTTPS in IE6. 153 | // concat is used to avoid the "Script URL" JSLint error: 154 | iframe 155 | .unbind('load') 156 | .prop('src', 'javascript'.concat(':false;')); 157 | } 158 | if (form) { 159 | form.remove(); 160 | } 161 | } 162 | }; 163 | } 164 | }); 165 | 166 | // The iframe transport returns the iframe content document as response. 167 | // The following adds converters from iframe to text, json, html, and script: 168 | $.ajaxSetup({ 169 | converters: { 170 | 'iframe text': function (iframe) { 171 | return iframe && $(iframe[0].body).text(); 172 | }, 173 | 'iframe json': function (iframe) { 174 | return iframe && $.parseJSON($(iframe[0].body).text()); 175 | }, 176 | 'iframe html': function (iframe) { 177 | return iframe && $(iframe[0].body).html(); 178 | }, 179 | 'iframe script': function (iframe) { 180 | return iframe && $.globalEval($(iframe[0].body).text()); 181 | } 182 | } 183 | }); 184 | 185 | })); 186 | -------------------------------------------------------------------------------- /spring-mvc-jquery-file-upload/src/main/webapp/js/myuploadfunction.js: -------------------------------------------------------------------------------- 1 | $(function () { 2 | $('#fileupload').fileupload({ 3 | dataType: 'json', 4 | 5 | done: function (e, data) { 6 | $("tr:has(td)").remove(); 7 | $.each(data.result, function (index, file) { 8 | 9 | 10 | $("#uploaded-files").append( 11 | $('') 12 | .append($('').text(file.fileName)) 13 | .append($('').text(file.fileSize)) 14 | .append($('').text(file.fileType)) 15 | .append($('').html("Click")) 16 | )//end $("#uploaded-files").append() 17 | }); 18 | }, 19 | 20 | progressall: function (e, data) { 21 | var progress = parseInt(data.loaded / data.total * 100, 10); 22 | $('#progress .bar').css( 23 | 'width', 24 | progress + '%' 25 | ); 26 | }, 27 | 28 | dropZone: $('#dropzone') 29 | }); 30 | }); -------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/articles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | Spring 5 | Spring 6 | JSON 7 | XML 8 | iText 9 | Apache POI 10 | http://hmkcode.com/spring-mvc-view-json-xml-pdf-or-excel/ 11 | Spring MVC View (JSON, XML, PDF or Excel) 12 |
13 |
14 | Java 15 | Java 16 | Castor 17 | XML 18 | Marshalling 19 | Unmarshalling 20 | http://hmkcode.com/castor-java-object-xml 21 | Castor - Java Object to XML & XML to Object Mapping 22 |
23 |
24 | Spring 25 | Spring 26 | jQuery 27 | File Upload 28 | http://hmkcode.com/spring-mvc-jquery-file-upload-multiple-dragdrop-progress/ 29 | Spring MVC + jQuery File Upload (multiple, drag & drop, progress…) 30 |
31 |
32 | Spring 33 | Spring 34 | JSON 35 | http://hmkcode.com/spring-mvc-json-json-to-java/ 36 | Spring MVC JSON (JSON to Java) 37 |
38 |
39 | Android 40 | Android 41 | Gradle 42 | http://hmkcode.com/android-studio-hello-world/ 43 | Android Studio – Hello World 44 |
45 |
-------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/get.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmkcode/Spring-Framework/c13c5146f6211377b2d6fd33de91ae12036d48bd/spring-mvc-json-pdf-xls-excel/get.pdf -------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.hmkcode 6 | spring-mvc-json-pdf-xls-excel 7 | war 8 | 1.0-SNAPSHOT 9 | 10 | spring-mvc-json-pdf-xls Maven Webapp 11 | http://maven.apache.org 12 | 13 | 14 | 15 | 3.1.0.RELEASE 16 | 4.11 17 | 18 | 19 | 20 | 21 | org.springframework 22 | spring-core 23 | ${spring.version} 24 | 25 | 26 | org.springframework 27 | spring-web 28 | ${spring.version} 29 | 30 | 31 | org.springframework 32 | spring-webmvc 33 | ${spring.version} 34 | 35 | 36 | org.springframework 37 | spring-oxm 38 | ${spring.version} 39 | 40 | 41 | 42 | javax.servlet 43 | servlet-api 44 | 2.5 45 | 46 | 47 | org.codehaus.jackson 48 | jackson-mapper-asl 49 | 1.9.12 50 | 51 | 52 | org.codehaus.jackson 53 | jackson-core-asl 54 | 1.9.12 55 | 56 | 57 | 58 | junit 59 | junit 60 | ${junit.version} 61 | test 62 | 63 | 64 | 65 | org.apache.poi 66 | poi 67 | 3.9 68 | 69 | 70 | org.apache.poi 71 | poi-ooxml 72 | 3.9 73 | 74 | 75 | com.itextpdf 76 | itextpdf 77 | 5.4.2 78 | 79 | 80 | org.codehaus.castor 81 | castor 82 | 1.2 83 | 84 | 85 | xerces 86 | xercesImpl 87 | 2.8.1 88 | 89 | 90 | 91 | spring-mvc-json-pdf-xls-excel 92 | 93 | 94 | org.mortbay.jetty 95 | maven-jetty-plugin 96 | 6.1.10 97 | 98 | 10 99 | 100 | 101 | 102 | maven-compiler-plugin 103 | 104 | 1.6 105 | 1.6 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/src/main/java/com/hmkcode/controllers/MyController.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.controllers; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.io.FileReader; 5 | import java.util.LinkedList; 6 | import org.exolab.castor.xml.MarshalException; 7 | import org.exolab.castor.xml.Unmarshaller; 8 | import org.exolab.castor.xml.ValidationException; 9 | import org.springframework.stereotype.Controller; 10 | import org.springframework.ui.Model; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | import org.springframework.web.bind.annotation.RequestMethod; 13 | 14 | import com.hmkcode.vo.Article; 15 | 16 | @Controller 17 | @RequestMapping("/controller") 18 | public class MyController { 19 | 20 | public MyController(){ 21 | System.out.println("Init MyController"); 22 | } 23 | 24 | @RequestMapping(value = "/get", method = RequestMethod.GET) 25 | public LinkedList
get(Model model) { 26 | 27 | FileReader reader; 28 | 29 | LinkedList
articles = null; 30 | 31 | try { 32 | 33 | reader = new FileReader("articles.xml"); 34 | //convert "unmarshal" data from XML "articles.xml" to Java object LinkedList
35 | articles = (LinkedList) Unmarshaller.unmarshal(LinkedList.class, reader); 36 | model.addAttribute("articles",articles); 37 | 38 | } catch (FileNotFoundException e) { 39 | e.printStackTrace(); 40 | } catch (MarshalException e) { 41 | e.printStackTrace(); 42 | } catch (ValidationException e) { 43 | e.printStackTrace(); 44 | } 45 | 46 | return articles; 47 | 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/src/main/java/com/hmkcode/view/ExcelView.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.view; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Map; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.apache.poi.ss.formula.functions.Hyperlink; 10 | import org.apache.poi.ss.usermodel.Cell; 11 | import org.apache.poi.ss.usermodel.CellStyle; 12 | import org.apache.poi.ss.usermodel.IndexedColors; 13 | import org.apache.poi.ss.usermodel.Row; 14 | import org.apache.poi.ss.usermodel.Sheet; 15 | import org.apache.poi.ss.usermodel.Workbook; 16 | 17 | import com.hmkcode.view.abstractview.AbstractExcelView; 18 | import com.hmkcode.vo.Article; 19 | 20 | public class ExcelView extends AbstractExcelView { 21 | 22 | @Override 23 | protected void buildExcelDocument(Map model, 24 | Workbook workbook, HttpServletRequest request, 25 | HttpServletResponse response) throws Exception { 26 | 27 | Sheet sheet = workbook.createSheet("sheet 1"); 28 | 29 | @SuppressWarnings("unchecked") 30 | LinkedList
articles = (LinkedList
) model.get("articles"); 31 | 32 | Row row = null; 33 | Cell cell = null; 34 | int r = 0; 35 | int c = 0; 36 | 37 | //Style for header cell 38 | CellStyle style = workbook.createCellStyle(); 39 | style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.index); 40 | style.setFillPattern(CellStyle.SOLID_FOREGROUND); 41 | style.setAlignment(CellStyle.ALIGN_CENTER); 42 | 43 | //Create header cells 44 | row = sheet.createRow(r++); 45 | 46 | cell = row.createCell(c++); 47 | cell.setCellStyle(style); 48 | cell.setCellValue("Title"); 49 | 50 | cell = row.createCell(c++); 51 | cell.setCellStyle(style); 52 | cell.setCellValue("URL"); 53 | 54 | cell = row.createCell(c++); 55 | cell.setCellStyle(style); 56 | cell.setCellValue("Categories"); 57 | 58 | cell = row.createCell(c++); 59 | cell.setCellStyle(style); 60 | cell.setCellValue("Tags"); 61 | 62 | 63 | //Create data cell 64 | for(Article article:articles){ 65 | row = sheet.createRow(r++); 66 | c = 0; 67 | row.createCell(c++).setCellValue(article.getTitle()); 68 | row.createCell(c++).setCellValue(article.getUrl()); 69 | row.createCell(c++).setCellValue(article.getCategories().toString()); 70 | row.createCell(c++).setCellValue(article.getTags().toString()); 71 | 72 | } 73 | for(int i = 0 ; i < 4; i++) 74 | sheet.autoSizeColumn(i, true); 75 | 76 | } 77 | 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/src/main/java/com/hmkcode/view/PDFView.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.view; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Map; 5 | import javax.servlet.http.HttpServletRequest; 6 | import javax.servlet.http.HttpServletResponse; 7 | import com.hmkcode.view.abstractview.AbstractPdfView; 8 | import com.hmkcode.vo.Article; 9 | import com.itextpdf.text.Anchor; 10 | import com.itextpdf.text.BaseColor; 11 | import com.itextpdf.text.Chunk; 12 | import com.itextpdf.text.Document; 13 | import com.itextpdf.text.Font; 14 | import com.itextpdf.text.Font.FontFamily; 15 | import com.itextpdf.text.pdf.PdfWriter; 16 | 17 | public class PDFView extends AbstractPdfView { 18 | 19 | protected void buildPdfDocument( 20 | Map model, 21 | Document document, 22 | PdfWriter writer, 23 | HttpServletRequest req, 24 | HttpServletResponse resp) 25 | throws Exception { 26 | 27 | 28 | // Get data "articles" from model 29 | @SuppressWarnings("unchecked") 30 | LinkedList
articles = (LinkedList
) model.get("articles"); 31 | 32 | // Fonts 33 | Font fontTitle = new Font(FontFamily.TIMES_ROMAN, 14, Font.BOLD, BaseColor.BLACK); 34 | Font fontTag = new Font(FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.WHITE); 35 | 36 | for(Article article:articles){ 37 | 38 | // 1.Title 39 | document.add(new Chunk("Title: ")); 40 | Chunk title = new Chunk(article.getTitle(), fontTitle); 41 | document.add(title); 42 | document.add(new Chunk(" ")); 43 | 44 | // -- newline 45 | document.add(Chunk.NEWLINE); 46 | 47 | // 2.URL 48 | document.add(new Chunk("URL: ")); 49 | Anchor url = new Anchor(article.getUrl()); 50 | url.setName(article.getUrl()); 51 | url.setReference(article.getUrl()); 52 | document.add(url); 53 | 54 | // -- newline 55 | document.add(Chunk.NEWLINE); 56 | 57 | // 3.Categories 58 | Chunk cat = null; 59 | document.add(new Chunk("Category: ")); 60 | for(String category:article.getCategories()){ 61 | cat = new Chunk(category, fontTag); 62 | cat.setBackground(new BaseColor(72, 121, 145), 1f, 0.5f, 1f, 1.5f); 63 | document.add(cat); 64 | document.add(new Chunk(" ")); 65 | 66 | 67 | } 68 | 69 | // -- newline 70 | document.add(Chunk.NEWLINE); 71 | 72 | // 4.Tags 73 | Chunk tg = null; 74 | document.add(new Chunk("Tags: ")); 75 | for(String tag:article.getTags()){ 76 | tg = new Chunk(tag, fontTag); 77 | tg.setBackground(new BaseColor(97, 97, 97), 1f, 0.5f, 1f, 1.5f); 78 | document.add(tg); 79 | document.add(new Chunk(" ")); 80 | 81 | } 82 | 83 | // -- newline 84 | document.add(Chunk.NEWLINE); 85 | document.add(Chunk.NEWLINE); 86 | 87 | } 88 | 89 | 90 | } 91 | 92 | 93 | 94 | 95 | } 96 | -------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/src/main/java/com/hmkcode/view/abstractview/AbstractExcelView.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.view.abstractview; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.util.Locale; 5 | import java.util.Map; 6 | 7 | import javax.servlet.ServletOutputStream; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | import org.apache.poi.ss.usermodel.Cell; 12 | import org.apache.poi.ss.usermodel.Row; 13 | import org.apache.poi.ss.usermodel.Sheet; 14 | import org.apache.poi.ss.usermodel.Workbook; 15 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; 16 | import org.springframework.core.io.Resource; 17 | import org.springframework.core.io.support.LocalizedResourceHelper; 18 | import org.springframework.web.servlet.support.RequestContextUtils; 19 | import org.springframework.web.servlet.view.AbstractView; 20 | 21 | public abstract class AbstractExcelView extends AbstractView { 22 | 23 | /** The content type for an Excel response */ 24 | private static final String CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 25 | 26 | /** The extension to look for existing templates */ 27 | private static final String EXTENSION = ".xlsx"; 28 | private String url; 29 | 30 | /** * Default Constructor. * Sets the content type of the view to "application/vnd.ms-excel". */ 31 | public AbstractExcelView() { 32 | setContentType(CONTENT_TYPE); 33 | } 34 | public void setUrl(String url) { 35 | this.url = url; 36 | } 37 | 38 | 39 | @Override 40 | protected boolean generatesDownloadContent() { 41 | return true; 42 | } 43 | 44 | /** 45 | * Renders the Excel view, given the specified model. 46 | */ 47 | @Override 48 | protected final void renderMergedOutputModel( 49 | Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { 50 | 51 | Workbook workbook; 52 | ByteArrayOutputStream baos = createTemporaryOutputStream(); 53 | /*if (this.url != null) { 54 | workbook = getTemplateSource(this.url, request); 55 | } 56 | else {*/ 57 | workbook = new XSSFWorkbook(); 58 | logger.debug("Created Excel Workbook from scratch"); 59 | //} 60 | 61 | buildExcelDocument(model, workbook, request, response); 62 | 63 | // Set the content type. 64 | //response.setContentType(getContentType()); 65 | 66 | // Should we set the content length here? 67 | // response.setContentLength(workbook.getBytes().length); 68 | 69 | // Flush byte array to servlet output stream. 70 | //ServletOutputStream out = response.getOutputStream(); 71 | workbook.write(baos); 72 | writeToResponse(response, baos); 73 | //out.flush(); 74 | } 75 | 76 | protected Workbook getTemplateSource(String url, HttpServletRequest request) throws Exception { 77 | LocalizedResourceHelper helper = new LocalizedResourceHelper(getApplicationContext()); 78 | Locale userLocale = RequestContextUtils.getLocale(request); 79 | Resource inputFile = helper.findLocalizedResource(url, EXTENSION, userLocale); 80 | 81 | // Create the Excel document from the source. 82 | if (logger.isDebugEnabled()) { 83 | logger.debug("Loading Excel workbook from " + inputFile); 84 | } 85 | //POIFSFileSystem fs = new POIFSFileSystem(inputFile.getInputStream()); 86 | return new XSSFWorkbook(inputFile.getInputStream()); 87 | } 88 | protected abstract void buildExcelDocument( 89 | Map model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) 90 | throws Exception; 91 | 92 | protected Cell getCell(Sheet sheet, int row, int col) { 93 | Row sheetRow = sheet.getRow(row); 94 | if (sheetRow == null) { 95 | sheetRow = sheet.createRow(row); 96 | } 97 | Cell cell = sheetRow.getCell(col); 98 | if (cell == null) { 99 | cell = sheetRow.createCell(col); 100 | } 101 | return cell; 102 | } 103 | protected void setText(Cell cell, String text) { 104 | cell.setCellType(Cell.CELL_TYPE_STRING); 105 | cell.setCellValue(text); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/src/main/java/com/hmkcode/view/abstractview/AbstractPdfView.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.view.abstractview; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.util.Map; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.springframework.web.servlet.view.AbstractView; 10 | 11 | import com.itextpdf.text.Document; 12 | import com.itextpdf.text.DocumentException; 13 | import com.itextpdf.text.pdf.PdfWriter; 14 | 15 | public abstract class AbstractPdfView extends AbstractView { 16 | 17 | 18 | public AbstractPdfView() { 19 | setContentType("application/pdf"); 20 | } 21 | @Override 22 | protected boolean generatesDownloadContent() { 23 | return true; 24 | } 25 | 26 | 27 | @Override 28 | protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { 29 | // IE workaround: write into byte array first. 30 | ByteArrayOutputStream baos = createTemporaryOutputStream(); 31 | 32 | // Apply preferences and build metadata. 33 | Document document = new Document(); 34 | PdfWriter writer = PdfWriter.getInstance(document, baos); 35 | prepareWriter(model, writer, request); 36 | buildPdfMetadata(model, document, request); 37 | 38 | // Build PDF document. 39 | writer.setInitialLeading(16); 40 | document.open(); 41 | buildPdfDocument(model, document, writer, request, response); 42 | document.close(); 43 | 44 | // Flush to HTTP response. 45 | writeToResponse(response, baos); 46 | 47 | 48 | } 49 | protected void prepareWriter(Map model, PdfWriter writer, HttpServletRequest request) 50 | throws DocumentException { 51 | writer.setViewerPreferences(getViewerPreferences()); 52 | } 53 | protected int getViewerPreferences() { 54 | return PdfWriter.ALLOW_PRINTING | PdfWriter.PageLayoutSinglePage; 55 | } 56 | protected void buildPdfMetadata(Map model, Document document, HttpServletRequest request) { 57 | } 58 | protected abstract void buildPdfDocument(Map model, Document document, PdfWriter writer, 59 | HttpServletRequest request, HttpServletResponse response) throws Exception; 60 | 61 | 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/src/main/java/com/hmkcode/vo/Article.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.vo; 2 | 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | 6 | public class Article { 7 | 8 | private String title; 9 | private String url; 10 | private List categories; 11 | private List tags; 12 | 13 | public String getTitle() { 14 | return title; 15 | } 16 | public void setTitle(String title) { 17 | this.title = title; 18 | } 19 | public String getUrl() { 20 | return url; 21 | } 22 | public void setUrl(String url) { 23 | this.url = url; 24 | } 25 | public List getCategories() { 26 | return categories; 27 | } 28 | public void setCategories(List categories) { 29 | this.categories = categories; 30 | } 31 | public List getTags() { 32 | return tags; 33 | } 34 | public void setTags(List tags) { 35 | this.tags = tags; 36 | } 37 | 38 | public void addCategory(String category){ 39 | if(this.categories == null) 40 | this.categories = new LinkedList(); 41 | this.categories.add(category); 42 | } 43 | public void addTag(String tag){ 44 | if(this.tags == null) 45 | this.tags = new LinkedList(); 46 | 47 | this.tags.add(tag); 48 | } 49 | @Override 50 | public String toString() { 51 | return "Article [title=" + title + ", url=" + url + ", categories=" 52 | + categories + ", tags=" + tags + "]"; 53 | } 54 | 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/src/main/webapp/WEB-INF/rest-servlet.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | SpringMVC 4 | 5 | index.html 6 | 7 | 8 | rest 9 | org.springframework.web.servlet.DispatcherServlet 10 | 11 | 12 | rest 13 | /rest/* 14 | 15 | -------------------------------------------------------------------------------- /spring-mvc-json-pdf-xls-excel/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Spring MVC - View (JSON, XML, PDF, Excel) 6 | 7 | 8 | 9 | 10 | 11 |

Spring MVC - View (JSON, XML, PDF, Excel)

12 |
13 | JSON 14 | XML 15 | PDF 16 | Excel 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /spring-mvc-json/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-mvc-json/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | spring-mvc-json 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.wst.jsdt.core.javascriptValidator 10 | 11 | 12 | 13 | 14 | org.eclipse.jdt.core.javabuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.wst.common.project.facet.core.builder 20 | 21 | 22 | 23 | 24 | org.eclipse.wst.validation.validationbuilder 25 | 26 | 27 | 28 | 29 | 30 | org.eclipse.jem.workbench.JavaEMFNature 31 | org.eclipse.wst.common.modulecore.ModuleCoreNature 32 | org.eclipse.wst.common.project.facet.core.nature 33 | org.eclipse.jdt.core.javanature 34 | org.eclipse.wst.jsdt.core.jsNature 35 | 36 | 37 | -------------------------------------------------------------------------------- /spring-mvc-json/README.md: -------------------------------------------------------------------------------- 1 | Spring MVC JSON 2 | =============== 3 | 4 | Spring MVC + JQuery + @RequestBody + JSON - making it work together 5 | ------------------------------------------------------------------- 6 | 7 | * return java object as json type. --> @ResponseBody 8 | 9 | @RequestMapping(method = RequestMethod.GET) 10 | public @ResponseBody Person helloWorld(HttpServletResponse res) { 11 | 12 | Person person = new Person(); 13 | person.setId(1); 14 | person.setName("hmk"); 15 | 16 | return person; 17 | } 18 | 19 | * Receive json data sent by POST request and map it into java object. --> @RequestBody 20 | 21 | @RequestMapping(value="person", method = RequestMethod.POST) 22 | public @ResponseBody Person getRequest( @RequestBody final Person person) { 23 | System.out.println(person.getId() + " " + person.getName()); 24 | return person; 25 | } 26 | -------------------------------------------------------------------------------- /spring-mvc-json/WebContent/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: 3 | 4 | -------------------------------------------------------------------------------- /spring-mvc-json/WebContent/WEB-INF/classes/com/hmkcode/controllers/RestController.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmkcode/Spring-Framework/c13c5146f6211377b2d6fd33de91ae12036d48bd/spring-mvc-json/WebContent/WEB-INF/classes/com/hmkcode/controllers/RestController.class -------------------------------------------------------------------------------- /spring-mvc-json/WebContent/WEB-INF/classes/com/hmkcode/vo/Person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hmkcode/Spring-Framework/c13c5146f6211377b2d6fd33de91ae12036d48bd/spring-mvc-json/WebContent/WEB-INF/classes/com/hmkcode/vo/Person.class -------------------------------------------------------------------------------- /spring-mvc-json/WebContent/WEB-INF/rest-servlet.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /spring-mvc-json/WebContent/WEB-INF/src/com/hmkcode/controllers/RestController.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.controllers; 2 | 3 | 4 | import javax.servlet.http.HttpServletResponse; 5 | 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.RequestBody; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestMethod; 10 | import org.springframework.web.bind.annotation.ResponseBody; 11 | 12 | import com.hmkcode.vo.Person; 13 | 14 | @Controller 15 | @RequestMapping("/cont") 16 | public class RestController { 17 | 18 | public RestController(){ 19 | System.out.println("init RestController"); 20 | } 21 | 22 | //this method responses to GET request http://localhost/spring-mvc-json/rest/cont 23 | // return Person object as json 24 | 25 | @RequestMapping(method = RequestMethod.GET) 26 | public @ResponseBody Person get(HttpServletResponse res) { 27 | 28 | Person person = new Person(); 29 | person.setId(1); 30 | person.setName("hmk"); 31 | 32 | return person; 33 | } 34 | 35 | //this method response to POST request http://localhost/spring-mvc-json/rest/cont/person 36 | // receives json data sent by client --> map it to Person object 37 | // return Person object as json 38 | @RequestMapping(value="person", method = RequestMethod.POST) 39 | public @ResponseBody Person post( @RequestBody final Person person) { 40 | 41 | System.out.println(person.getId() + " " + person.getName()); 42 | return person; 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /spring-mvc-json/WebContent/WEB-INF/src/com/hmkcode/vo/Person.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.vo; 2 | 3 | import java.io.Serializable; 4 | 5 | 6 | public class Person implements Serializable { 7 | 8 | /** 9 | * 10 | */ 11 | private static final long serialVersionUID = 1L; 12 | private int id; 13 | private String name; 14 | public int getId() { 15 | return id; 16 | } 17 | public void setId(int id) { 18 | this.id = id; 19 | } 20 | public String getName() { 21 | return name; 22 | } 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /spring-mvc-json/WebContent/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | SpringMVC 4 | 5 | index.html 6 | index.htm 7 | index.jsp 8 | default.html 9 | default.htm 10 | default.jsp 11 | 12 | 13 | 14 | rest 15 | 16 | 17 | org.springframework.web.servlet.DispatcherServlet 18 | 19 | 20 | 21 | rest 22 | /rest/* 23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-mvc-json/WebContent/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Insert title here 6 | 7 | 31 | 32 | 33 | SpringMVC 34 | 35 | 36 | -------------------------------------------------------------------------------- /spring-mvc-json/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.springframeworkspring-webmvc3.0.5.RELEASE 4 | 5 | 6 | org.codehaus.jacksonjackson-mapper-asl1.4.2 7 | 8 | 9 | -------------------------------------------------------------------------------- /spring-mvc-root-webapplicationcontext/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /spring-mvc-root-webapplicationcontext/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.hmkcode 6 | spring-mvc-root-webapplicationcontext 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | spring-mvc-root-webapplicationcontext 11 | http://maven.apache.org 12 | 13 | 14 | 4.0.0.RELEASE 15 | 4.11 16 | 17 | 18 | 19 | 20 | org.springframework 21 | spring-core 22 | ${spring.version} 23 | 24 | 25 | org.springframework 26 | spring-web 27 | ${spring.version} 28 | 29 | 30 | org.springframework 31 | spring-webmvc 32 | ${spring.version} 33 | 34 | 35 | javax.servlet 36 | servlet-api 37 | 2.5 38 | 39 | 40 | org.codehaus.jackson 41 | jackson-mapper-asl 42 | 1.9.12 43 | 44 | 45 | org.codehaus.jackson 46 | jackson-core-asl 47 | 1.9.12 48 | 49 | 50 | 51 | junit 52 | junit 53 | ${junit.version} 54 | test 55 | 56 | 57 | 58 | 59 | spring-mvc-regex 60 | 61 | 62 | org.eclipse.jetty 63 | jetty-maven-plugin 64 | 9.2.2.v20140723 65 | 66 | 67 | 68 | org.apache.maven.plugins 69 | maven-compiler-plugin 70 | 3.1 71 | 72 | 1.7 73 | 1.7 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /spring-mvc-root-webapplicationcontext/src/main/java/com/hmkcode/spring/beans/RootBean.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.beans; 2 | 3 | public class RootBean { 4 | 5 | @Override 6 | public String toString() { 7 | return "RootBean"; 8 | } 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /spring-mvc-root-webapplicationcontext/src/main/java/com/hmkcode/spring/controllers/MyController.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.controllers; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | import org.springframework.web.bind.annotation.ResponseBody; 9 | 10 | import com.hmkcode.spring.beans.RootBean; 11 | 12 | 13 | 14 | @Controller 15 | @RequestMapping("/controller") 16 | public class MyController { 17 | 18 | public MyController(){ 19 | System.out.println("new Controller"); 20 | } 21 | 22 | @Autowired 23 | ApplicationContext context; 24 | 25 | @RequestMapping(value = "/get", method = RequestMethod.GET) 26 | public @ResponseBody String get() { 27 | 28 | RootBean rootBean = (RootBean) context.getBean("rootBean"); 29 | System.out.println(rootBean); 30 | 31 | return rootBean.toString(); 32 | } 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /spring-mvc-root-webapplicationcontext/src/main/webapp/WEB-INF/rest-servlet.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /spring-mvc-root-webapplicationcontext/src/main/webapp/WEB-INF/root-context.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /spring-mvc-root-webapplicationcontext/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | SpringMVC 4 | 5 | index.html 6 | 7 | 8 | 9 | 10 | contextConfigLocation 11 | /WEB-INF/root-context.xml 12 | 13 | 14 | org.springframework.web.context.ContextLoaderListener 15 | 16 | 17 | 18 | 19 | rest 20 | org.springframework.web.servlet.DispatcherServlet 21 | 22 | 23 | 24 | rest 25 | /rest/* 26 | 27 | -------------------------------------------------------------------------------- /spring-mvc-root-webapplicationcontext/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Root Context 6 | 7 | 8 | 9 | 10 | 11 | Call Controller 12 | 13 | -------------------------------------------------------------------------------- /spring-mvc-root-webapplicationcontext/src/test/java/com/hmkcode/spring/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /spring-mvc-security/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-mvc-security/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.hmkcode 6 | spring-mvc-security 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | spring-mvc-security 11 | http://maven.apache.org 12 | 13 | 14 | 3.1.0.RELEASE 15 | 16 | 17 | 18 | 19 | 20 | 21 | org.springframework 22 | spring-core 23 | ${spring.version} 24 | 25 | 26 | 27 | org.springframework 28 | spring-web 29 | ${spring.version} 30 | 31 | 32 | 33 | org.springframework 34 | spring-webmvc 35 | ${spring.version} 36 | 37 | 38 | 39 | 40 | org.springframework.security 41 | spring-security-core 42 | ${spring.version} 43 | 44 | 45 | 46 | org.springframework.security 47 | spring-security-web 48 | ${spring.version} 49 | 50 | 51 | 52 | org.springframework.security 53 | spring-security-config 54 | ${spring.version} 55 | 56 | 57 | 58 | 59 | javax.servlet 60 | javax.servlet-api 61 | 3.1.0 62 | 63 | 64 | 65 | 66 | spring-mvc-security 67 | 68 | 69 | org.eclipse.jetty 70 | jetty-maven-plugin 71 | 9.0.4.v20130625 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /spring-mvc-security/src/main/java/com/hmkcode/controllers/RestController.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.controllers; 2 | 3 | 4 | import org.springframework.security.core.context.SecurityContextHolder; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.ui.ModelMap; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestMethod; 9 | 10 | @Controller 11 | @RequestMapping("/controller") 12 | public class RestController { 13 | 14 | public RestController(){ 15 | System.out.println("init RestController"); 16 | } 17 | 18 | // URL: /rest/controller/any 19 | @RequestMapping(value="/any", method = RequestMethod.GET) 20 | public String any(ModelMap model) { 21 | 22 | model.addAttribute("message", "This URL can be accessed by any one"); 23 | return "hello"; 24 | 25 | } 26 | 27 | // URL: /rest/controller/user 28 | @RequestMapping(value="/user", method = RequestMethod.GET) 29 | public String user(ModelMap model) { 30 | String user = SecurityContextHolder.getContext().getAuthentication().getName(); 31 | 32 | model.addAttribute("message", "This URL can be accessed only by authorized user "+user); 33 | 34 | return "hello"; 35 | } 36 | 37 | } 38 | 39 | -------------------------------------------------------------------------------- /spring-mvc-security/src/main/webapp/WEB-INF/rest-servlet.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 | 14 | 15 | 16 | / 17 | 18 | 19 | .jsp 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /spring-mvc-security/src/main/webapp/WEB-INF/spring-security.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-mvc-security/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | SPRING MVC SECURITY 8 | 9 | index.html 10 | 11 | 12 | 13 | 14 | rest 15 | org.springframework.web.servlet.DispatcherServlet 16 | 17 | 18 | rest 19 | /rest/* 20 | 21 | 22 | 23 | 24 | org.springframework.web.context.ContextLoaderListener 25 | 26 | 27 | 28 | 29 | contextConfigLocation 30 | 31 | /WEB-INF/rest-servlet.xml, 32 | /WEB-INF/spring-security.xml 33 | 34 | 35 | 36 | 37 | 38 | springSecurityFilterChain 39 | org.springframework.web.filter.DelegatingFilterProxy 40 | 41 | 42 | 43 | springSecurityFilterChain 44 | /* 45 | 46 | 47 | -------------------------------------------------------------------------------- /spring-mvc-security/src/main/webapp/hello.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Message : ${message}

4 | 5 | -------------------------------------------------------------------------------- /spring-mvc-test/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-mvc-test/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.hmkcode 6 | spring-mvc-test 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | spring-mvc-test 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 5.0.8.RELEASE 16 | 4.12 17 | 4.0.1 18 | 2.9.6 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | org.springframework 27 | spring-context 28 | ${spring.version} 29 | 30 | 31 | 32 | org.springframework 33 | spring-core 34 | ${spring.version} 35 | 36 | 37 | 38 | org.springframework 39 | spring-beans 40 | ${spring.version} 41 | 42 | 43 | 44 | org.springframework 45 | spring-web 46 | ${spring.version} 47 | 48 | 49 | 50 | org.springframework 51 | spring-webmvc 52 | ${spring.version} 53 | 54 | 55 | 56 | org.springframework 57 | spring-expression 58 | ${spring.version} 59 | 60 | 61 | org.springframework 62 | spring-test 63 | ${spring.version} 64 | test 65 | 66 | 67 | 68 | 69 | com.fasterxml.jackson.core 70 | jackson-core 71 | ${jackson.version} 72 | 73 | 74 | com.fasterxml.jackson.core 75 | jackson-databind 76 | ${jackson.version} 77 | 78 | 79 | 80 | junit 81 | junit 82 | ${junit.version} 83 | test 84 | 85 | 86 | 87 | 88 | commons-logging 89 | commons-logging 90 | 1.2 91 | 92 | 93 | 94 | 95 | javax.servlet 96 | javax.servlet-api 97 | ${javax.servlet.version} 98 | provided 99 | 100 | 101 | 102 | 103 | org.hamcrest 104 | hamcrest-library 105 | 1.3 106 | test 107 | 108 | 109 | com.jayway.jsonpath 110 | json-path 111 | 2.2.0 112 | test 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | org.eclipse.jetty 121 | jetty-maven-plugin 122 | 9.4.11.v20180605 123 | 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /spring-mvc-test/src/main/java/com/hmkcode/controllers/Controller.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.controllers; 2 | 3 | 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestMethod; 6 | import org.springframework.web.bind.annotation.ResponseBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import com.hmkcode.model.Link; 10 | 11 | @RestController 12 | @RequestMapping("api") 13 | public class Controller { 14 | 15 | @RequestMapping(value = "/get", method = RequestMethod.GET) 16 | public @ResponseBody Link get(){ 17 | System.out.println("Get!"); 18 | 19 | Link link = new Link(); 20 | link.setTitle("HMKCODE BLOG!"); 21 | link.setUrl("hmkcode.com"); 22 | 23 | return link; 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /spring-mvc-test/src/main/java/com/hmkcode/model/Link.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.model; 2 | 3 | public class Link { 4 | 5 | private String title; 6 | private String url; 7 | 8 | public Link(){ 9 | 10 | } 11 | public Link(String title, String url){ 12 | this.title = title; 13 | this.url = url; 14 | } 15 | public String getTitle() { 16 | return title; 17 | } 18 | public void setTitle(String title) { 19 | this.title = title; 20 | } 21 | public String getUrl() { 22 | return url; 23 | } 24 | public void setUrl(String url) { 25 | this.url = url; 26 | } 27 | @Override 28 | public String toString() { 29 | return "Link [title=" + title + ", url=" + url + "]"; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-mvc-test/src/main/webapp/WEB-INF/rest-servlet.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /spring-mvc-test/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | SPRING MVC 8 | 9 | index.html 10 | 11 | 12 | 13 | 14 | rest 15 | org.springframework.web.servlet.DispatcherServlet 16 | 17 | 18 | rest 19 | /rest/* 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /spring-mvc-test/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Spring MVC Test 6 | 7 | 8 |

Spring MVC Test | MockMVC

9 | GET JSON 10 | 11 | -------------------------------------------------------------------------------- /spring-mvc-test/src/test/java/com/hmkcode/test/TestController.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.test; 2 | 3 | import javax.servlet.ServletContext; 4 | 5 | import org.junit.Assert; 6 | import org.junit.Before; 7 | import org.junit.Ignore; 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.mock.web.MockServletContext; 12 | import org.springframework.test.context.ContextConfiguration; 13 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 14 | import org.springframework.test.context.web.WebAppConfiguration; 15 | import org.springframework.test.web.servlet.MockMvc; 16 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 17 | import org.springframework.web.context.WebApplicationContext; 18 | 19 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 20 | import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; 21 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 22 | 23 | @RunWith(SpringJUnit4ClassRunner.class) 24 | @ContextConfiguration(locations = "/rest-servlet.xml") 25 | @WebAppConfiguration 26 | public class TestController { 27 | 28 | @Autowired 29 | private WebApplicationContext wac; 30 | 31 | private MockMvc mockMvc; 32 | @Before 33 | public void setup() throws Exception { 34 | this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); 35 | } 36 | @Ignore 37 | @Test 38 | public void configTest() { 39 | ServletContext servletContext = wac.getServletContext(); 40 | 41 | Assert.assertNotNull(servletContext); 42 | Assert.assertTrue(servletContext instanceof MockServletContext); 43 | Assert.assertNotNull(wac.getBean("controller")); 44 | } 45 | 46 | @Test 47 | public void test() throws Exception{ 48 | this.mockMvc.perform(get("/api/get")) 49 | .andExpect(status().isOk()) 50 | .andDo(print()); 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /spring-mvc-test/src/test/resources/rest-servlet.xml: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring-mybatis-junit/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /spring-mybatis-junit/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.hmkcode 6 | spring-mybatis-junit 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | spring-mybatis-junit 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 3.1.0.RELEASE 16 | 17 | 18 | 19 | 20 | com.h2database 21 | h2 22 | 1.3.171 23 | 24 | 25 | junit 26 | junit 27 | 4.11 28 | test 29 | 30 | 31 | org.mybatis 32 | mybatis 33 | 3.2.1 34 | 35 | 36 | org.mybatis 37 | mybatis-spring 38 | 1.2.0 39 | 40 | 41 | org.springframework 42 | spring-core 43 | ${org.springframework.version} 44 | 45 | 46 | org.springframework 47 | spring-expression 48 | ${org.springframework.version} 49 | 50 | 51 | org.springframework 52 | spring-beans 53 | ${org.springframework.version} 54 | 55 | 56 | org.springframework 57 | spring-aop 58 | ${org.springframework.version} 59 | 60 | 61 | org.springframework 62 | spring-context 63 | ${org.springframework.version} 64 | 65 | 66 | org.springframework 67 | spring-context-support 68 | ${org.springframework.version} 69 | 70 | 71 | org.springframework 72 | spring-tx 73 | ${org.springframework.version} 74 | 75 | 76 | org.springframework 77 | spring-jdbc 78 | ${org.springframework.version} 79 | 80 | 81 | org.springframework 82 | spring-orm 83 | ${org.springframework.version} 84 | 85 | 86 | org.springframework 87 | spring-oxm 88 | ${org.springframework.version} 89 | 90 | 91 | 92 | org.springframework 93 | spring-asm 94 | ${org.springframework.version} 95 | 96 | 97 | org.springframework 98 | spring-context-support 99 | ${org.springframework.version} 100 | 101 | 102 | org.springframework 103 | spring-test 104 | ${org.springframework.version} 105 | test 106 | 107 | 108 | 109 | 110 | 111 | maven-compiler-plugin 112 | 2.0.2 113 | 114 | 1.6 115 | 1.6 116 | 117 | 118 | 119 | org.codehaus.mojo 120 | exec-maven-plugin 121 | 122 | 123 | my-execution 124 | compile 125 | 126 | java 127 | 128 | 129 | 130 | 131 | com.hmkcode.spring.mybatis.App 132 | 133 | 134 | 135 | 136 | 137 | -------------------------------------------------------------------------------- /spring-mybatis-junit/src/main/java/com/hmkcode/spring/mybatis/App.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mybatis; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.commons.logging.Log; 6 | import org.apache.commons.logging.LogFactory; 7 | import org.springframework.context.ApplicationContext; 8 | import org.springframework.context.support.ClassPathXmlApplicationContext; 9 | 10 | import com.hmkcode.spring.mybatis.service.Service; 11 | import com.hmkcode.spring.mybatis.vo.Child; 12 | import com.hmkcode.spring.mybatis.vo.Parent; 13 | 14 | 15 | public class App 16 | { 17 | private static Log log = LogFactory.getLog(App.class); 18 | 19 | public static void main( String[] args ) 20 | { 21 | 22 | ApplicationContext cxt = new ClassPathXmlApplicationContext("spring-config.xml"); 23 | Service service = (Service) cxt.getBean("service"); 24 | 25 | log.info("Running App..."); 26 | 27 | //( 1 ) SELECT ALL PARENTS 28 | System.out.println("( 1 ) selectAllParent()"); 29 | List parents = service.selectAllParent(); 30 | System.out.println("-> "+parents); 31 | 32 | System.out.println("****************************************************"); 33 | 34 | //( 2 ) SELECT ONE PARENT 35 | System.out.println("( 2 ) selectParent(2)"); 36 | Parent parent = service.selectParent(2); 37 | System.out.println("-> "+parent); 38 | 39 | System.out.println("****************************************************"); 40 | 41 | //( 3 ) INSERT ONE PARENT 42 | System.out.println("( 3 ) insertParent(parent)"); 43 | parent = new Parent(); 44 | parent.setParentName("parent-4 inserted"); 45 | service.insertParent(parent); 46 | System.out.println("-> parent inserted"); 47 | 48 | System.out.println("****************************************************"); 49 | 50 | //( 4 ) SELECT ALL CHILDREN OF A GIVEN PARENT 51 | System.out.println("( 4 ) selectAllChildren(parentId)"); 52 | List children = service.selectAllChildren(1); 53 | System.out.println("-> "+children); 54 | 55 | System.out.println("****************************************************"); 56 | 57 | //( 5 ) SELECT ONE CHILD 58 | System.out.println("( 5 ) selectChild(1)"); 59 | Child child = service.selectChild(1); 60 | System.out.println("-> "+child); 61 | 62 | System.out.println("****************************************************"); 63 | 64 | //( 6 ) INSERT ONE CHILD 65 | System.out.println("( 6 ) selectAllChildren(parentId)"); 66 | child = new Child(); 67 | child.setChildName("child-4 inserted"); 68 | child.setParentId(2); 69 | service.insertchild(child); 70 | System.out.println("-> child inserted"); 71 | 72 | System.out.println("****************************************************"); 73 | 74 | //( 7 ) SELECT ALL PARENTS (AGAIN!) 75 | System.out.println("( 7 ) selectAllParent()"); 76 | parents = service.selectAllParent(); 77 | System.out.println("-> "+parents); 78 | 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /spring-mybatis-junit/src/main/java/com/hmkcode/spring/mybatis/mapper/Mapper.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mybatis.mapper; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Insert; 6 | import org.apache.ibatis.annotations.Param; 7 | import org.apache.ibatis.annotations.Select; 8 | 9 | import com.hmkcode.spring.mybatis.vo.Child; 10 | import com.hmkcode.spring.mybatis.vo.Parent;; 11 | 12 | 13 | public interface Mapper { 14 | 15 | //if @Select is used table/column name and class/property name should be the same 16 | 17 | 18 | //SQL query in "Mapper.xml" 19 | public List selectAllParent(); 20 | 21 | //SQL query in "Mapper.xml" 22 | public Parent selectParent(@Param("id") int id); 23 | 24 | @Insert("INSERT INTO parent (parentName) VALUES (#{parentName})") 25 | public int insertParent(Parent parent); 26 | 27 | @Select("SELECT * FROM child WHERE parentId = #{id}") 28 | public List selectAllChildren(@Param("id") int id); 29 | 30 | @Select("SELECT * FROM child WHERE childId = #{id}") 31 | public Child selectChild(@Param("id") int id); 32 | 33 | //SQL query in "Mapper.xml" 34 | public int insertchild(Child child); 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /spring-mybatis-junit/src/main/java/com/hmkcode/spring/mybatis/service/Service.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mybatis.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | 7 | import com.hmkcode.spring.mybatis.mapper.Mapper; 8 | import com.hmkcode.spring.mybatis.vo.Child; 9 | import com.hmkcode.spring.mybatis.vo.Parent; 10 | 11 | 12 | public class Service { 13 | 14 | @Autowired 15 | private Mapper mapper; 16 | 17 | public List selectAllParent(){ 18 | return mapper.selectAllParent(); 19 | } 20 | 21 | public Parent selectParent(int id){ 22 | return mapper.selectParent(id); 23 | } 24 | 25 | public int insertParent(Parent parent){ 26 | return mapper.insertParent(parent); 27 | } 28 | 29 | public List selectAllChildren(int parnetId){ 30 | return mapper.selectAllChildren(parnetId); 31 | } 32 | 33 | public Child selectChild(int id){ 34 | return mapper.selectChild(id); 35 | } 36 | 37 | public int insertchild(Child child){ 38 | return mapper.insertchild(child); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-mybatis-junit/src/main/java/com/hmkcode/spring/mybatis/vo/Child.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mybatis.vo; 2 | 3 | import org.apache.ibatis.type.Alias; 4 | 5 | /** 6 | * Hello world! 7 | * 8 | */ 9 | @Alias("Child") 10 | public class Child extends Parent 11 | { 12 | private int childId; 13 | private String childName; 14 | 15 | 16 | public int getChildId() { 17 | return childId; 18 | } 19 | public void setChildId(int childId) { 20 | this.childId = childId; 21 | } 22 | public String getChildName() { 23 | return childName; 24 | } 25 | public void setChildName(String childName) { 26 | this.childName = childName; 27 | } 28 | public String toString(){ 29 | return "Child [ id: "+this.childId+" - name: "+this.childName+" (PARENT_ID :"+this.getParentId()+") ]"; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-mybatis-junit/src/main/java/com/hmkcode/spring/mybatis/vo/Parent.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mybatis.vo; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.type.Alias; 6 | 7 | @Alias("Parent") 8 | public class Parent { 9 | 10 | private int parentId; 11 | private String parentName; 12 | 13 | private List children; 14 | 15 | 16 | public int getParentId() { 17 | return parentId; 18 | } 19 | 20 | public void setParentId(int parentId) { 21 | this.parentId = parentId; 22 | } 23 | 24 | public String getParentName() { 25 | return parentName; 26 | } 27 | 28 | public void setParentName(String parentName) { 29 | this.parentName = parentName; 30 | } 31 | 32 | public List getChildren() { 33 | return children; 34 | } 35 | 36 | public void setChildren(List children) { 37 | this.children = children; 38 | } 39 | 40 | public String toString(){ 41 | return "\nParent [ id: "+this.parentId+" - name: "+this.parentName+" \n" + 42 | "\tchildren: "+this.children+ 43 | "\n]"; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /spring-mybatis-junit/src/main/resources/com/hmkcode/spring/mybatis/mapper/Mapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 28 | 29 | 30 | 33 | 34 | 35 | 36 | INSERT INTO child 37 | (parentId,childName) 38 | VALUES 39 | (#{parentId},#{childName}) 40 | 41 | 42 | -------------------------------------------------------------------------------- /spring-mybatis-junit/src/main/resources/com/hmkcode/spring/mybatis/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 14 | 15 | -------------------------------------------------------------------------------- /spring-mybatis-junit/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE parent (parentId INT PRIMARY KEY AUTO_INCREMENT, parentName VARCHAR(255)); 2 | CREATE TABLE child (childId INT PRIMARY KEY AUTO_INCREMENT, parentId INT, childName VARCHAR(255)); -------------------------------------------------------------------------------- /spring-mybatis-junit/src/main/resources/spring-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 23 | 24 | 25 | 26 | 27 | 28 | 34 | 35 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /spring-mybatis-junit/src/main/resources/test-data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO parent (parentName) VALUES ('parent-1') 2 | INSERT INTO parent (parentName) VALUES ('parent-2') 3 | INSERT INTO parent (parentName) VALUES ('parent-3') 4 | 5 | INSERT INTO child (childName,parentId) VALUES ('child-1',1) 6 | INSERT INTO child (childName,parentId) VALUES ('child-2',1) 7 | INSERT INTO child (childName,parentId) VALUES ('child-3',2) 8 | 9 | -------------------------------------------------------------------------------- /spring-mybatis-junit/src/test/java/com/hmkcode/spring/mybatis/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mybatis; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.commons.logging.Log; 6 | import org.apache.commons.logging.LogFactory; 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | import com.hmkcode.spring.mybatis.service.Service; 14 | import com.hmkcode.spring.mybatis.vo.Parent; 15 | 16 | 17 | import static org.junit.Assert.assertNotNull; 18 | import static org.junit.Assert.assertTrue;; 19 | 20 | /** 21 | * Unit test for simple App. 22 | */ 23 | 24 | @RunWith(SpringJUnit4ClassRunner.class) 25 | @ContextConfiguration(locations = "/spring-config.xml") 26 | public class AppTest { 27 | 28 | private static Log log = LogFactory.getLog(AppTest.class); 29 | 30 | @Autowired private Service service; 31 | 32 | 33 | @Test 34 | public void testSelectAllParent(){ 35 | log.info("testing selectAllParent()..."); 36 | 37 | List parents = service.selectAllParent(); 38 | 39 | assertNotNull("failure - parents is null", parents); 40 | 41 | assertTrue("failure - expected 3 parents", parents.size() == 3); 42 | 43 | assertTrue("failure - expected 2 children", parents.get(0).getChildren().size() == 2); 44 | 45 | log.info(parents); 46 | } 47 | 48 | @Test 49 | public void testSelectParent(){ 50 | log.info("testing selectParent()..."); 51 | int parentId = 2; 52 | String parentName = "parent-2"; 53 | Parent parent = service.selectParent(parentId); 54 | 55 | assertNotNull("failure - parents is null", parent); 56 | 57 | assertTrue("failure - parent name is not right", parent.getParentName().equals(parentName)); 58 | 59 | assertTrue("failure - expected 1 child", parent.getChildren().size() == 1); 60 | } 61 | 62 | @Test 63 | public void testInsertParent(){ 64 | 65 | log.info("testing insertParent()..."); 66 | 67 | int parentId = 4; 68 | String parentName = "parent-4 inserted"; 69 | 70 | Parent parent = new Parent(); 71 | parent.setParentName(parentName); 72 | 73 | service.insertParent(parent); 74 | 75 | parent = service.selectParent(4); 76 | 77 | assertNotNull("failure - parents is null", parent); 78 | 79 | assertTrue("failure - parent name is not right", parent.getParentName().equals(parentName)); 80 | 81 | assertTrue("failure - expected 0 child", parent.getChildren().size() == 0); 82 | 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /spring-mybatis-junit/src/test/resources/spring-config-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /spring-mybatis/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /spring-mybatis/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.hmkcode 6 | spring-mybatis 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | spring-mybatis 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 3.1.0.RELEASE 16 | 17 | 18 | 19 | 20 | com.h2database 21 | h2 22 | 1.3.171 23 | 24 | 25 | junit 26 | junit 27 | 4.11 28 | test 29 | 30 | 31 | org.mybatis 32 | mybatis 33 | 3.2.1 34 | 35 | 36 | org.mybatis 37 | mybatis-spring 38 | 1.2.0 39 | 40 | 41 | org.springframework 42 | spring-core 43 | ${org.springframework.version} 44 | 45 | 46 | org.springframework 47 | spring-expression 48 | ${org.springframework.version} 49 | 50 | 51 | org.springframework 52 | spring-beans 53 | ${org.springframework.version} 54 | 55 | 56 | org.springframework 57 | spring-aop 58 | ${org.springframework.version} 59 | 60 | 61 | org.springframework 62 | spring-context 63 | ${org.springframework.version} 64 | 65 | 66 | org.springframework 67 | spring-context-support 68 | ${org.springframework.version} 69 | 70 | 71 | org.springframework 72 | spring-tx 73 | ${org.springframework.version} 74 | 75 | 76 | org.springframework 77 | spring-jdbc 78 | ${org.springframework.version} 79 | 80 | 81 | org.springframework 82 | spring-orm 83 | ${org.springframework.version} 84 | 85 | 86 | org.springframework 87 | spring-oxm 88 | ${org.springframework.version} 89 | 90 | 91 | org.springframework 92 | spring-web 93 | ${org.springframework.version} 94 | 95 | 96 | org.springframework 97 | spring-webmvc 98 | ${org.springframework.version} 99 | 100 | 101 | org.springframework 102 | spring-webmvc-portlet 103 | ${org.springframework.version} 104 | 105 | 106 | org.springframework 107 | spring-asm 108 | ${org.springframework.version} 109 | 110 | 111 | org.springframework 112 | spring-context-support 113 | ${org.springframework.version} 114 | 115 | 116 | 117 | 118 | 119 | maven-compiler-plugin 120 | 2.0.2 121 | 122 | 1.6 123 | 1.6 124 | 125 | 126 | 127 | org.codehaus.mojo 128 | exec-maven-plugin 129 | 130 | 131 | my-execution 132 | compile 133 | 134 | java 135 | 136 | 137 | 138 | 139 | com.hmkcode.spring.mybatis.App 140 | 141 | 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /spring-mybatis/src/main/java/com/hmkcode/spring/mybatis/App.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mybatis; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.commons.logging.Log; 6 | import org.apache.commons.logging.LogFactory; 7 | import org.springframework.context.ApplicationContext; 8 | import org.springframework.context.support.ClassPathXmlApplicationContext; 9 | 10 | import com.hmkcode.spring.mybatis.service.Service; 11 | import com.hmkcode.spring.mybatis.vo.Person; 12 | 13 | 14 | public class App 15 | { 16 | private static Log log = LogFactory.getLog(App.class); 17 | 18 | public static void main( String[] args ) 19 | { 20 | 21 | ApplicationContext cxt = new ClassPathXmlApplicationContext("spring-config.xml"); 22 | Service service = (Service) cxt.getBean("service"); 23 | 24 | log.info("Running App..."); 25 | 26 | System.out.println("List persons = service.selectAllPerson()"); 27 | List persons = service.selectAllPerson(); 28 | System.out.println("-> "+persons+"\n"); 29 | 30 | System.out.println("Person person = service.selectPerson(2)"); 31 | Person person = service.selectPerson(2); 32 | System.out.println("-> "+person+"\n"); 33 | 34 | System.out.println("service.insertPerson(person)"); 35 | person.setName("Inserted person"); 36 | service.insertPerson(person); 37 | System.out.println("-> inserted..."+"\n"); 38 | 39 | System.out.println("List persons = service.selectAllPerson()"); 40 | persons = service.selectAllPerson(); 41 | System.out.println("-> "+persons+"\n"); 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /spring-mybatis/src/main/java/com/hmkcode/spring/mybatis/mapper/Mapper.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mybatis.mapper; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Insert; 6 | import org.apache.ibatis.annotations.Param; 7 | import org.apache.ibatis.annotations.Select; 8 | 9 | import com.hmkcode.spring.mybatis.vo.Person; 10 | 11 | 12 | public interface Mapper { 13 | 14 | /** 15 | * Retrieves the List of person 16 | * @return person list 17 | */ 18 | //SQL query using xml "Mapper.xml" 19 | public List selectAllPerson(); 20 | 21 | 22 | /** 23 | * Retrieves a person 24 | * @param person id 25 | * @return person 26 | */ 27 | //SQL query using annotation 28 | @Select("SELECT * FROM person") 29 | public List selectAllPerson2(); 30 | 31 | /****************************************** 32 | * Retrieves a person 33 | * @param person id 34 | * @return person 35 | */ 36 | //SQL query using annotation 37 | @Select("SELECT * FROM person WHERE id = #{id}") 38 | public Person selectPerson(@Param("id") int id); 39 | 40 | /****************************************** 41 | * Insert a person 42 | * @param person 43 | * @return int 44 | */ 45 | @Insert("INSERT INTO person (name) VALUES (#{name})") 46 | public int insertPerson(Person person); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /spring-mybatis/src/main/java/com/hmkcode/spring/mybatis/service/Service.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mybatis.service; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | 10 | import com.hmkcode.spring.mybatis.mapper.Mapper; 11 | import com.hmkcode.spring.mybatis.vo.Person; 12 | 13 | public class Service { 14 | 15 | @Autowired 16 | private Mapper mapper; 17 | 18 | 19 | public List selectAllPerson() { 20 | return mapper.selectAllPerson(); 21 | } 22 | public Person selectPerson(int id) { 23 | return mapper.selectPerson(id); 24 | } 25 | public int insertPerson(Person person){ 26 | return mapper.insertPerson(person); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-mybatis/src/main/java/com/hmkcode/spring/mybatis/vo/Person.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mybatis.vo; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class Person 8 | { 9 | 10 | private int id; 11 | private String name; 12 | 13 | public int getId() { 14 | return id; 15 | } 16 | public void setId(int id) { 17 | this.id = id; 18 | } 19 | public String getName() { 20 | return name; 21 | } 22 | public void setName(String name) { 23 | this.name = name; 24 | } 25 | public String toString(){ 26 | return "id: "+id+" - name: "+name; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-mybatis/src/main/resources/com/hmkcode/spring/mybatis/mapper/Mapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 | -------------------------------------------------------------------------------- /spring-mybatis/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE person (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255)); 2 | -------------------------------------------------------------------------------- /spring-mybatis/src/main/resources/spring-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 23 | 24 | 25 | 26 | 27 | 28 | 34 | 35 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /spring-mybatis/src/main/resources/test-data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO person (name) VALUES ('person-1') 2 | INSERT INTO person (name) VALUES ('person-2') 3 | INSERT INTO person (name) VALUES ('person-3') 4 | INSERT INTO person (name) VALUES ('person-4') 5 | INSERT INTO person (name) VALUES ('person-5') 6 | INSERT INTO person (name) VALUES ('person-6') 7 | -------------------------------------------------------------------------------- /spring-mybatis/src/test/java/com/hmkcode/spring/mybatis/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring.mybatis; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /spring-profile/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring-profile/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.hmkcode 6 | spring-profile 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | spring-profile 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 3.1.0.RELEASE 16 | 17 | 18 | 19 | 20 | junit 21 | junit 22 | 4.11 23 | test 24 | 25 | 26 | org.springframework 27 | spring-core 28 | ${org.springframework.version} 29 | 30 | 31 | org.springframework 32 | spring-beans 33 | ${org.springframework.version} 34 | 35 | 36 | org.springframework 37 | spring-context 38 | ${org.springframework.version} 39 | 40 | 41 | org.springframework 42 | spring-context-support 43 | ${org.springframework.version} 44 | 45 | 46 | cglib 47 | cglib-nodep 48 | 2.2 49 | compile 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /spring-profile/src/main/java/com/hmkcode/App.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode; 2 | 3 | import org.springframework.context.support.ClassPathXmlApplicationContext; 4 | import com.hmkcode.vo.Person; 5 | 6 | public class App 7 | { 8 | public static void main( String[] args ) 9 | { 10 | 11 | System.setProperty("spring.profiles.active", "development"); //or production 12 | ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config-all.xml"); 13 | 14 | Person person = (Person)ctx.getBean("person"); 15 | System.out.println("Person: "+person); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-profile/src/main/java/com/hmkcode/spring/SpringConfig.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.spring; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.Profile; 6 | 7 | import com.hmkcode.vo.Person; 8 | 9 | @Configuration 10 | @Profile("development") 11 | public class SpringConfig { 12 | 13 | @Bean 14 | public Person person(){ 15 | return new Person(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-profile/src/main/java/com/hmkcode/vo/Person.java: -------------------------------------------------------------------------------- 1 | package com.hmkcode.vo; 2 | 3 | public class Person { 4 | 5 | private int id; 6 | private String name; 7 | public int getId() { 8 | return id; 9 | } 10 | public void setId(int id) { 11 | this.id = id; 12 | } 13 | public String getName() { 14 | return name; 15 | } 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | 20 | public String toString(){ 21 | return "Id: "+id+" - Name: "+name; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-profile/src/main/resources/spring-config-all.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /spring-profile/src/main/resources/spring-config-development.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /spring-profile/src/main/resources/spring-config-production.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | --------------------------------------------------------------------------------