我想使用Hibernate选择单个列而不是整个对象.到目前为止我有这个:
List<String> firstname = null;
firstname = getSession().createCriteria(People.class).list();
Run Code Online (Sandbox Code Playgroud)
我的问题是上面的代码将整个People表作为对象返回,而不仅仅是"firstname".我不知道如何指定只返回"firstname"而不是整个对象.
如果我这样做:
File f = new File("c:\\text.txt");
if (f.exists()) {
System.out.println("File exists");
} else {
System.out.println("File not found!");
}
Run Code Online (Sandbox Code Playgroud)
然后创建文件并始终返回"文件存在".是否可以在不创建文件的情况下检查文件是否存在?
编辑:
我忘了提到它是在for循环中.所以这是真实的事情:
for (int i = 0; i < 10; i++) {
File file = new File("c:\\text" + i + ".txt");
System.out.println("New file created: " + file.getPath());
}
Run Code Online (Sandbox Code Playgroud) 使用PHP,我想创建一个while循环,它读取一个大文件并在请求时发送当前行号.使用Ajax,我想获取当前行数并将其打印到页面上.使用html按钮,我希望能够单击并激活或终止仅运行ONCE并调用ajax方法的javascript线程.
我已经给了它一个镜头,但由于某种原因,除非我注释掉该echo str_repeat(' ',1024*64);函数并且当它被注释掉时它没有打印,它显示了整个循环结果:
1行已处理.2行已处理.3行已处理.4行已处理.5行已处理.6行已处理.7行已处理.8已处理的行.9行已处理.10行已处理.
在一行中,而不是在单独的行中显示它们,如:
1 row(s) processed.
2 row(s) processed.
3 row(s) processed.
4 row(s) processed.
5 row(s) processed.
6 row(s) processed.
7 row(s) processed.
8 row(s) processed.
9 row(s) processed.
10 row(s) processed.
Run Code Online (Sandbox Code Playgroud)
另外我不知道如何终止JavaScript线程.总共有2个问题:
1. It's returning the entire While loop object at once instead of each time it loops.
2. I'm not sure how to terminate the JQuery thread.
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?以下是我的代码.
msgserv.php
<?php
//Initiate Line Count
$lineCount = …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个非常简单的代码来发挥音效.到目前为止,我有这个代码:
SoundManager snd;
int combo;
private void soundSetup() {
// Create an instance of the sound manger
snd = new SoundManager(getApplicationContext());
// Set volume rocker mode to media volume
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the samples from res/raw
combo = snd.load(R.raw.combo);
}
private void playSound() {
soundSetup();
snd.play(combo);
}
Run Code Online (Sandbox Code Playgroud)
但是,由于某种原因,当我使用该playSound()方法时,没有任何反应.音频文件位于正确的位置.
我注意到有几个问题询问这个话题.我查看了它们,我无法将它们应用到我特定的Spring设置中.我想根据用户的角色将我的登录重定向配置为有条件的.这是我到目前为止:
<http auto-config="true" use-expressions="true">
<custom-filter ref="filterSecurityInterceptor" before="FILTER_SECURITY_INTERCEPTOR"/>
<access-denied-handler ref="accessDeniedHandler"/>
<form-login
login-page="/login"
default-target-url="/admin/index"
authentication-failure-url="/index?error=true"
/>
<logout logout-success-url="/index" invalidate-session="true"/>
</http>
Run Code Online (Sandbox Code Playgroud)
我认为这个问题可能与我想要做的事情在同一条线上.有人知道我怎么能申请吗?
编辑1
<bean id="authenticationProcessingFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManager" />
<property name="authenticationSuccessHandler" ref="authenticationSuccessHandler"/>
</bean>
<bean id="authenticationSuccessHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
<property name="defaultTargetUrl" value="/login.jsp"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
编辑2
目前我没有像这个例子中public class Test implements AuthenticationSuccessHandler {}所示的类.
如何使用@SizeMySQL DECIMAL(x,y)列的注释?
我正在使用BigDecimal,但是当我尝试包含@Size max它时它不起作用.这是我的代码:
@Size(max = 7,2)
@Column(name = "weight")
private BigDecimal weight;
Run Code Online (Sandbox Code Playgroud) 有人可以保存这两个文件并运行它们并告诉我为什么我收到错误"ob_flush()[ref.outcontrol]:无法刷新缓冲区.没有缓冲区要刷新".我试着用Google搜索,它说我必须使用ob_start(); 但是当我这样做时,它不会逐行打印,而是在完成后从FOR循环返回整个对象.我是PHP的新手,所以我不知道还有什么地方要看..
test_process.php
// This script will write numbers from 1 to 100 into file
// And sends continuously info to user
$fp = fopen( '/tmp/output.txt', 'w') or die('Failed to open');
set_time_limit( 120);
ignore_user_abort(true);
for( $i = 0; $i < 100; $i++){
echo "<script type=\"text/javascript\">parent.document.getElementById( 'foo').innerHTML += 'Line $i<br />';</script>";
echo str_repeat( ' ', 2048);
flush();
ob_flush();
sleep(1);
fwrite( $fp, "$i\n");
}
fclose( $fp);
Run Code Online (Sandbox Code Playgroud)
main.html中
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css" media="screen">
.msg{ background:#aaa;padding:.2em; border-bottom:1px #000 solid}
.new{ …Run Code Online (Sandbox Code Playgroud) 使用嵌入式tomcat,这段代码:
System.out.println("getServletPath: " + request.getServletPath());
System.out.println("getServletContext: " + request.getServletContext().getContextPath());
System.out.println("getServerName: " + request.getServerName());
System.out.println("getServerPort: " + request.getServerPort());
Run Code Online (Sandbox Code Playgroud)
打印出来:
getServletPath: /example
getServletContext:
getServerName: localhost
getServerPort: 9090
Run Code Online (Sandbox Code Playgroud)
这是否意味着:
request.getRequestDispatcher("/example/read.jsp").forward(request, response);
Run Code Online (Sandbox Code Playgroud)
将这个URL forward(request, response)看到JSP:
http://localhost:9090/example/read.jsp?
有没有办法打印出绝对 URL getRequestDispatcher("relativePath")正在寻址的内容?
目前我在每个 Controller方法中都复制了这些代码:
Transaction transaction = HibernateUtil.getSessionFactory().getCurrentSession().getTransaction();
if (!HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().isActive()) {
transaction.begin();
}
Run Code Online (Sandbox Code Playgroud)
这是正确的方法,还是有更好的方法,也许在我可以参考的单独的课程中?如果是这样,怎么样?每当我试图将它放在一个单独的类中并从其他类引用它时,它就失败了.
编辑:我正在尝试使用尽可能少的外部库.如果Java在JDK中内置了ORM/JPA实现,我就不会使用Hibernate
这有效:
String webappDir = "...";
context = tomcat.addWebapp("/", new File(webappDir).getAbsolutePath());
Run Code Online (Sandbox Code Playgroud)
这不是:
context = tomcat.addContext("/", new File("").getAbsolutePath());
Run Code Online (Sandbox Code Playgroud)
webappDir在这个实例中我真的不需要,因为我没有提供任何JSP页面或客户端资源,我只是使用response.getWriter().println(...);服务器端.
抛出没有异常,websocket根本无法打开.
我可以假设这是一个tomcat错误吗?