Skip to content

Instantly share code, notes, and snippets.

View yanhua365's full-sized avatar

yanhua365 yanhua365

View GitHub Profile
@yanhua365
yanhua365 / XmlResponseController.java
Last active December 28, 2015 09:29
Spring MVC 3.1 的RequestMapping注解里增加了produces属性, 可以明确指定返回响应的格式为特定的格式,比如XML格式。
@RequestMapping(value = "/only-xml-response", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public Object onlyXml(ModelMap modelMap){
//BookDTO 是增加了JAXB注解的一个JavaBean
BookDTO b = new BookDTO("Java in Action");
return b;
//也可以返回字符串
//return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><book><name>java in action.</name></book>";
}
@yanhua365
yanhua365 / XmlResponseController.groovy
Last active December 28, 2015 09:29
返回XML响应的Spring Boot CLI脚本。
package org.test
@Grab("spring-boot-starter-actuator")
@RestController
public class XmlResponseController {
@RequestMapping(value="/xml", method = RequestMethod.GET,produces = MediaType.APPLICATION_XML_VALUE)
public def hello() {
//实际应用的时候可以用MarkupBuilder来构造
@yanhua365
yanhua365 / DownloadController.java
Last active December 28, 2015 23:09
Spring MVC 提供文件下载的控制器
@RequestMapping(value = "/download/{filename:.+}", method = RequestMethod.GET)
public void download(@PathVariable("filename") String filename,HttpServletRequest request ,HttpServletResponse response){
try {
File file=new File(request.getSession().getServletContext().getRealPath("/download/"+filename));
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;filename="+ filename);
response.setHeader("Content-Length", "" + file.length());
//System.out.println(file.getAbsolutePath());
@yanhua365
yanhua365 / JavaBeanEqualsBaseOnNotNullFields.java
Created November 25, 2013 08:05
用IntelliJ IDEA生成的equals和hashCode方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UpdateConfig that = (UpdateConfig) o;
if (!fromVersion.equals(that.fromVersion)) return false;
if (!targetVersion.equals(that.targetVersion)) return false;
@yanhua365
yanhua365 / JavaBeanEqualsByGuava.java
Created November 25, 2013 08:19
用Guava简化equals,hashCode和toString的编写。
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UpdateConfig that = (UpdateConfig) o;
return Objects.equal(fromVersion, that.fromVersion)
&& Objects.equal(targetVersion, that.targetVersion);
}
@yanhua365
yanhua365 / git-completion.bash
Last active December 29, 2015 09:39
新安装Git后,配置中文编码以及用户名和电子邮件等信息
#在%GIT_HOME%\etc\git-completion.bash 这个文件中添加
alias ls='ls --show-control-chars --color=auto'
@yanhua365
yanhua365 / gist:7652058
Created November 26, 2013 01:34
新建Git项目并push到对应的oschina的仓库中
mkdir myprj
cd myprj
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://git.oschina.net/yourname/myprj.git
git push -u origin master
@yanhua365
yanhua365 / gist:7652773
Created November 26, 2013 02:52
用Guava实现一行代码从classpath中读取文件内容
String json = Resources.toString(Resources.getResource("com/mycompany/test/fixture/config_fixture.json"), Charsets.UTF_8);
@yanhua365
yanhua365 / spring-jpa-datasource-h2.xml
Created November 26, 2013 07:52
Spring JPA中的各种数据库连接配置
<!-- H2的混合模式 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver" />
<!--<property name="url" value="jdbc:h2:~/databases/mydb" />-->
<property name="url" value="jdbc:h2:file://D:\h2\mydb;AUTO_SERVER=TRUE;USER=SA;" />
</bean>
<!-- H2的内存模式 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
@yanhua365
yanhua365 / config_edit.ftl
Last active December 29, 2015 15:19
Freemarker和Bootstrap结合高亮显示单个输入验证错误的消息
<#--
* 结合bootstrap的样式,有错误的时候高亮显示字段
* config是表单绑定的模型对象,targetVersion是它的一个属性
-->
<@spring.bind "config.targetVersion"/>
<#assign error>
<#if spring.status.errorMessages?has_content>error</#if>
</#assign>
<div class="control-group ${error}">
<label class="control-label" for="inputTargetVersion">升级后版本</label>