我是Spring MVC的新手,但我对它的功能印象深刻.
我正在使用3.1.0-RELEASE,我必须显示PDF以响应表单:表单提交.
这是我在控制器中编写的(小)代码:
@RequestMapping(value = "new_product", method = RequestMethod.POST, params = "print")
@ResponseBody
public void saveAndShowPDF(ModelMap map, ShippingRequestInfo requestInfo, HttpServletRequest request, HttpServletResponse httpServletResponse) throws IOException {
saveProductChanges(map, requestInfo, request, httpServletResponse);
httpServletResponse.setContentType("application/pdf");
byte[] pdfImage = productService.getPDFImage(requestInfo.getRequestId());
httpServletResponse.getOutputStream().write(pdfImage);
}
Run Code Online (Sandbox Code Playgroud)
此代码将PDF byte []发送回原始窗口.
如何让PDF显示在单独的窗口中,以便我仍然可以使用原始浏览器窗口显示其他内容?最好的方法是使用客户端PDF查看程序(Adobe Reader,FoxIt等)显示PDF,但我可以将PDF显示在单独的浏览器窗口中.
编辑: 我决定设置Content-Disposition,以便浏览器显示一个保存/打开框,用户可以打开Adobe(丢失主浏览器页面).
httpServletResponse.setHeader("Content-Disposition","attachment;filename=cool.pdf");
Run Code Online (Sandbox Code Playgroud)
感谢大家!
实际上,我有这个功能,我有一个框架,我设置URL(ip:port/birt/preview?__report=report.rptdesign&__format=pdf¶meters...),该框架呈现PDF文件.
但我想隐藏这个URL ...
我需要返回一个带有Spring MVC的PDF文件,但该PDF是由另一个应用程序生成的.
这意味着我得到了另一个应用程序(Eclipse Birt Engine),我通过URL(ip:port/birt/preview?__report=report.rptdesign&__format=pdf¶meters...)传递参数,它生成一个PDF文件,我需要从我的控制器获取该PDF并将其与Spring MVC一起返回.有人可以帮忙吗?
我有一个关于Spring + Thymeleaf日期格式的问题.我有一个简单的实体与LocalDate date字段.我希望从表单中的用户获取此日期并将其保存到MySQL数据库.我收到这样的错误:
无法将类型为java.lang.String的属性值转换为属性日期所需的类型java.time.LocalDate; 嵌套异常是org.springframework.core.convert.ConversionFailedException:无法从类型java.lang.String转换为类型java.time.LocalDate,值为2019-04-30; 嵌套异常是java.time.format.DateTimeParseException:无法在索引2处解析文本2019-04-30
我的实体:
@Entity
@Table(name="game")
public class Game{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Transient
private User gameOwner;
private LocalDate date;
private LocalTime time;
//other fields
Run Code Online (Sandbox Code Playgroud)
Thymeleaf观点/形式:
<form action="#" th:action="@{/games/addForm}" th:object="${gameForm}" method="post">
<p>Date: <input type="date" th:field="*{date}" /></p>
</form>
Run Code Online (Sandbox Code Playgroud)
这个问题的原因是什么?也许还有其他更好的存储日期方式?
每当我将 LocalDate 持久化到 MySQL 数据库时,日期都会存储一天(2017 年 11 月 11 日变为 2017 年 11 月 10 日)。我已经尝试在 MySQL 服务器上的应用程序中设置时区并将 legacyDateTimeCode 设置为 false 但问题仍然存在。关于如何修复它的任何想法?如果我切换到本地 h2 数据库,则日期存储正确。
Spring-boot-starter-parent: 1.5.7 hibernate 5.2.10 mysql: 5.7 LocalDate 存储在 DATE 字段中
我已实现UserDetailsService,它返回一个实例MyUser(实现UserDetails)
public MyUser loadUserByUsername(String arg0)
Run Code Online (Sandbox Code Playgroud)
现在我想MyUser在我的JSP页面中访问我的自定义getter/fields ,到目前为止我得到了这个:
${pageContext.request.userPrincipal.name}
Run Code Online (Sandbox Code Playgroud)
但这只允许访问Principal对象.我怎样才能访问MyUser对象?
所以我已经注意到没有简单的解决方案可以on delete set null与 JPA一起使用,但我听说可能有解决方法。我的问题是下一个(这里有点抽象,但我希望它仍然有意义):
想象一下有员工。每个员工都知道他们的老板,但反过来就不知道了。老板本身就是员工。
我想要完成的事情:
如果老板被解雇,在他手下工作的每个人都会失去他的老板(显然)。
@Entity
@Table(name = "employee")
public class Employee
{
@Column (name = "id" )
private String id;
@OneToOne
@JoinColumn( name = "boss" )
private employee boss;
}
Run Code Online (Sandbox Code Playgroud)
这是它在 SQL 中的工作方式:
ALTER TABLE EMPLOYEE
ADD CONSTRAINT CONSTRAINTNAME FOREIGN KEY (BOSS)
REFERENCES EMPLOYEE (ID)
ON DELETE SET NULL;
Run Code Online (Sandbox Code Playgroud)
我不希望没有老板的人也被解雇,我只是希望他们不再提及他们以前的老板。我看到了使用 @PreRemove 的解决方案,但我不确定这是否与我打算的一样。
提前致谢!
我是JavaScript的学习者,对Mapbox GL JS有疑问:我在Mapbox Studio中有一种样式,其中一层是我的图层-“位置”。我已将其添加为图块。这一层有两个GeoJSON点,但是我无法在GL JS中获得它们。我发现我应该使用method querySourceFeatures(sourceID, [parameters]),但是在正确填充其参数方面存在问题。我写:
var allFeatures = map.querySourceFeatures('_id-of-my-tyleset_', {
'sourceLayer': 'locations'
});
Run Code Online (Sandbox Code Playgroud)
..并且不起作用。
更有趣的是,稍后在代码中我将这一层与method一起使用queryRenderedFeatures,这没关系:
map.on('click', function(e) {
var features = map.queryRenderedFeatures(e.point, {
layers: ['locations']
});
if (!features.length) {
return;
}
var feature = features[0];
flyToPoint(feature);
var popup = new mapboxgl.Popup({
offset: [0, -15]
})
.setLngLat(feature.geometry.coordinates)
.setHTML('<h3>' + feature.properties.name + '</h3>' + '<p>' +
feature.properties.description + '</p>')
.addTo(map);
});
Run Code Online (Sandbox Code Playgroud)
我已经阅读了很多有关在地图上添加图层的信息,并且知道答案很简单,但是我无法实现该解决方案,请提供帮助:)
这是 GitHub上的项目。
我正在寻找一个UserDetails在Spring Security 3 中制作自定义对象的示例.我希望有人可以提供帮助,谢谢.
如何获取位于服务器目录结构中文件中的pdf,以便在浏览器中为Spring MVC应用程序的用户加载pdf ?
我已经在Google上进行了搜索,并发现了有关如何生成PDF的帖子,但是在这种情况下他们的答案不起作用。例如,此其他发布不相关,因为res.setContentType("application/pdf");在我下面的代码中不能解决问题。另外,此其他文章描述了如何从数据库执行此操作,但未显示完整的工作控制器代码。其他帖子也有类似的问题,导致它们在这种情况下无法工作。
我只需要提供一个文件(而不是来自数据库),并让用户在其浏览器中可以查看它。我想出的最好的是下面的代码,它要求用户下载PDF或在浏览器之外的单独应用程序中查看它。我可以对下面的特定代码进行哪些特定更改,以便用户在单击链接时自动看到其浏览器中的PDF内容,而不提示您下载该PDF内容?
@RequestMapping(value = "/test-pdf")
public void generatePdf(HttpServletRequest req,HttpServletResponse res){
res.setContentType("application/pdf");
res.setHeader("Content-Disposition", "attachment;filename=report.pdf");
ServletOutputStream outStream=null;
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(new File("/path/to", "nameOfThe.pdf")));
/*ServletOutputStream*/ outStream = res.getOutputStream();
//to make it easier to change to 8 or 16 KBs
int FILE_CHUNK_SIZE = 1024 * 4;
byte[] chunk = new byte[FILE_CHUNK_SIZE];
int bytesRead = 0;
while ((bytesRead = bis.read(chunk)) != -1) {outStream.write(chunk, 0, bytesRead);}
bis.close();
outStream.flush();
outStream.close(); …Run Code Online (Sandbox Code Playgroud)