Home Php C# Sql C C++ Javascript Python Java Go Android Git Linux Asp.net Django .net Node.js Ios Xcode Cocoa Iphone Mysql Tomcat Mongodb Bash Objective-c Scala Visual-studio Apache Elasticsearch Jar Eclipse Jquery Ruby-on-rails Ruby Rubygems Android-studio Spring Lua Sqlite Emacs Ubuntu Perl Docker Swift Amazon-web-services Svn Html Ajax Xml Java-ee Maven Intellij-idea Rvm Macos Unix Css Ipad Postgresql Css3 Json Windows-server Vue.js Typescript Oracle Hibernate Internet-explorer Github Tensorflow Laravel Symfony Redis Html5 Google-app-engine Nginx Firefox Sqlalchemy Lucene Erlang Flask Vim Solr Webview Facebook Zend-framework Virtualenv Nosql Ide Twitter Safari Flutter Bundle Phonegap Centos Sphinx Actionscript Tornado Register | Login | Edit Tags | New Questions | 繁体 | 简体


10 questions online user: 24

0
votes
answers
18 views
+10

Struts 2空字符串動作

-1

使用Struts2,在執行任何操作時會執行任何操作,當提交時 該表單將如何發佈?Struts 2空字符串動作

<s:form action="" method="POST" namespace="mynamespace" onSubmit="return false;"></s:form> 
+0

除非您在瀏覽器中關閉javascript或重寫該事件,否則此表單永遠不會發布。如果更改代碼以使表單即使未在動作屬性中指定動作名稱,也更好。 –

沙发
0
0

對HTML5的W3規範非常明確提到

的動作和formaction內容屬性,如果指定,必須有 的值是一個有效的非空URL由 空間可能包圍。

這裏的參考:

https://www.w3.org/TR/html5/forms.html#attr-fs-formaction

但是,如果你不寫action="",這取決於瀏覽器,你會最終都提交表單到你在同樣的動作。

0
votes
answers
15 views
+10

獲取日期格式化並在Angular 2中打印到屏幕App

0

我想在我的Angular應用程序中使用日期管道以在輸入中的模板中使用它時正確解析出日期管道。最初,格式化日期之前,代碼是這樣的:獲取日期格式化並在Angular 2中打印到屏幕App

<input class="app-input" [readonly]="!hasAdminAccess"         
    [(ngModel)]="staff.profile.hireDate" placeholder="None" 
    [field-status]="getPropertyStatus('profile.hireDate')"/> 

我的日期管得到最接近的是這樣的:

<input class="app-input" 
{{ staff.profile.hireDate | date:'shortDate' }} placeholder="None" 
[field-status]="getPropertyStatus('profile.hireDate')"/> 

但是,打印的看法是這樣的(從字面上此):

> <input class="app-input" 3/18/2014 placeholder="None" 
> [field-status]="getPropertyStatus('profile.hireDate')"/> 

現在,你會發現,正確格式的日期是存在的(和日期轉化成功發生,使它這樣:

3/18/2014 

但是,我不想休息(顯然)。我該如何重新修改這裏的語法以便獲得打印日期?我盯着它,並嘗試了一些調整,但迄今還沒有得到它的工作。

+1

value ='{{staff.profile.hireDate | date:'shortDate'}}'在你的輸入標籤中 – getbuckts

沙发
0
0

您可以使用typcript和ngModelChanged屬性中的get和set函數在設置後修改ngModel

組件模板:

<input class="app-input" [(ngModel)]="hireDate" (ngModelChange)="dateChanged($event)"/> 

組件類:

import { Component } from '@angular/core'; 
import { DatePipe } from '@angular/common'; 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <div> 
     <button (click)="setDate()">Set Date</button> 
     <input class="app-input" readonly="true" [(ngModel)]="hireDate" (ngModelChange)="dateChanged($event)" placeholder="None"/> 
    </div> 
    `, 
}) 
export class App { 
    name:string; 
    staff: any; 
    myDate: any; 
    private _hireDate; 

    dateChanged(value) { 
    this.hireDate = this.datePipe.transform(value, 'shortDate'); 
    } 

    set hireDate(value) { 
    this._hireDate = this.datePipe.transform(value, 'shortDate'); 
    } 

    get hireDate() { 
    return this._hireDate; 
    } 

    setDate() { 
    this.hireDate = '10-03-1993'; 
    } 

    constructor(private datePipe: DatePipe) { 
    } 
} 

輸入的值將被設置,只要輸入的變化,所以它可能會導致UX問題,因爲用戶將無法輸入他的首選日期。解決方法是每當用戶輸入日期時調用日期更改函數。 (例如,通過點擊按鈕)。

我相信set和get函數只能用於類變量,在你的情況下你有一個對象屬性。如下所示修改設置功能將起作用。

set hireDate(value) { 
    this._hireDate = this.datePipe.transform(value, 'shortDate'); 
    this.staff.profile.hireDate = this._hireDate; 
    } 

我還添加了一個plunkr here

0
votes
answers
34 views
+10

如何使用JpaRepository和嵌套的對象列表進行搜索?

0

說明如何使用JpaRepository和嵌套的對象列表進行搜索?

有一個PersonRepositoryPerson實體, Person類包含List<Qualification>Qualification類有3個簡單的字段。

我嘗試添加自定義方法@Query註釋和使用JPQL得到的結果,但Qualification類領域是不可用於操縱JPQL因爲庫本身包含List<Qualification>代替的Qualification只是一個簡單的領域。

如何通過這些Qualification的嵌套字段進行搜索?

查詢

現在我需要找人實體的列表,其中合格的experienceInMonths大於3小於9和資格的名字字段='Java的。

代碼

Person.java

@Data 
@Entity 
public class Person { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private String id; 

@NotEmpty 
@Size(min = 2) 
private String name; 

@NotEmpty 
@Size(min = 2) 
private String surname; 

@ElementCollection(targetClass = java.util.ArrayList.class, fetch = FetchType.EAGER) 
private List<Qualification> qualifications = new ArrayList<>(); 

} 

PersonRepository.java

@Repository 
public interface PersonRepository extends JpaRepository<Person, String> { 
} 

Qualification.java

@Data 
@AllArgsConstructor 
public class Qualification implements Serializable { 

    @Id @GeneratedValue 
    private String id; 
    private String name; 
    private String experienceInMonths; 
} 

編輯:不重複的this post,因爲這裏是嵌套對象的集合。不只是單一的參考。

+0

的可能的複製[春季數據JPA找到嵌入對象的屬性(HTTPS ://stackoverflow.com/questions/24441411/spring-data-jpa-find-by-embedded-object-property) –

+0

正如在編輯帖子中提到的,這個問題並沒有相應的提出答案。正如你可以在粘貼的代碼中看到的那樣,對象的'List'有一個問題 - 不僅僅是其中的一個。 – DevDio

沙发
0
1

首先,將experienceInMonthsString更改爲int(否則您無法將該字符串與數字進行比較)。然後,你可以嘗試使用這個「香腸」:

List<Person> findByQualifications_experienceInMonthsGreaterThanAndQualifications_experienceInMonthsLessThanAndName(int experienceGreater, int experienceLess, String name); 

或者你可以嘗試使用這種相當不錯的方法:

@Query("select p from Person p left join p.qualifications q where q.experienceInMonths > ?1 and q.experienceInMonths < ?2 and q.name = ?3") 
List<Person> findByQualification(int experienceGreater, int experienceLess, String name); 
+0

是否在當地爲您工作? JPQL版本給了我'org.hibernate.QueryException:不能解引用標量集合元素:'和香腸之一:'引起:java.lang.IllegalStateException:非法嘗試解引用基本類型的路徑源[null]。即使將Qualification#experienceInMonths類型更改爲'int' – DevDio

+0

@DevDio嘗試將'@ Entity'添加到'Qualification'並將'@ ElementCollection'替換爲'@ OneToMany'(類似的變體適用於我...) – Cepr0

+0

好的,終於搞定了,但它需要在Hibernate中實現單獨的'Qualification'表。其中包含FK給Person(@Entity,@ManyToOne附加字段)+ QualificationRepository是強制性的。 還測試了建議的方法,JPQL的版本工作正常,但'香腸'的名稱需要調整爲: 'findByQualifications_experienceInMonthsGreaterThanAndQualifications_experienceInMonthsLessThanAndQualifications_Name'。 否則會搜索人名。非常感謝@ Cepr0的幫助! – DevDio

0
votes
answers
31 views
+10

計時器在活動關閉並重新啓動後不會停止

0

我有一個每秒更新一次textview的runnable計時器,當活動處於onStop(或稱爲後臺)計時器繼續運行時。我遇到的問題是,當我重新啓動活動時,它會再次啓動相同的計時器,因此數字會以其應有的速度提高兩倍。我把它編碼,這樣它會在重新啓動它們之前殺死兩個計時器,但我相信當活動再次啓動時,計時器不會被殺死。下面是我的代碼示例:計時器在活動關閉並重新啓動後不會停止

t.cancel(); 
    cd.cancel(); 
    t = new Timer(); 
    t.schedule(new TimerTask() { 

     @Override 
     public void run() { 
      runOnUiThread(new Runnable() { 

       public void run() { 

那只是一小部分,但它應該終止計時器(t.cancel();),然後開始一個新的,當活動停止這只是發生和然後重新啓動。請幫助這個問題讓我絕對瘋了。

============================================== ===========

對於願意讀很多的勇敢的靈魂,這裏要說的是我有問題我的整個活動:

public class PayTracker extends Activity { 
    private static double Reserve; 
    private static int Reserve1; 
    public static double money; 
    public static double counter; 
    private static int go; 
    private static int countdown; 
    public static int convert; 
    public static double HW; 
    public static double OTW; 
    public static double HPD; 
    public static double DPPS; 
    public Timer t = new Timer(); 
    public Timer cd = new Timer(); 
    public static String mcountdown = "Time till overtime"; 
    public static String mmoney = "total cash"; 
    public static String mcounter = "ticks"; 
    public static String mReserve = "building total"; 
    public static String mReserve1 = "building total 2"; 
    public static String mHW; 
    public static String mOTW; 
    public static String mHPD; 
    public static String mDPPS; 
    public static String mgo; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_pay_tracker); 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
    // Receive messages from options page 
     double pHW, pOTW, pHPD; 
     Intent intent = getIntent(); 
     pHW = intent.getDoubleExtra(Options.MESSAGE_HW, 0); 
     pOTW = intent.getDoubleExtra(Options.MESSAGE_OTW, 0); 
     pHPD = intent.getDoubleExtra(Options.MESSAGE_HPD, 0); 
     if(pHW != 0){ 
      HW = pHW; 
      OTW = pOTW; 
      HPD = pHPD; 
     } 
    // Color buttons 
     Button buttonc = (Button) findViewById(R.id.clockin); 
     buttonc.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY); 
     Button buttond = (Button) findViewById(R.id.clockout); 
     buttond.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY); 

//  go = 0; 
     // Calculate pay per second 
     final double PPS = (HW/3600); 
     DPPS = (PPS/50); 
     final double OTPPS = (OTW/3600); 
     final double DOTPPS = (OTPPS/50); 
     final double HPDPS = (HPD*3600); 
     final double DHPDPS = (HPDPS*50); 
     // Display 

     final TextView t1 = (TextView) findViewById(R.id.yourpay); 
     t1.setTextColor(Color.parseColor("#008000")); 
     final TextView t2 = (TextView) this.findViewById(R.id.payper); 
     final String result2 = String.format("%.8f", OTPPS); 
     final String result = String.format("%.8f", PPS); 

     // if(go != 1){ 
     // go = 1; 
     // if(go == 1){ 
     t.cancel(); 
     cd.cancel(); 
     // go = 0; 
     // } 
     // if(go == 0){ 
     // go = 1; 
     t = new Timer(); 
     t.schedule(new TimerTask() { 

      @Override 
      public void run() { 
       runOnUiThread(new Runnable() { 

        public void run() { 
         if(DHPDPS==0){ 
          money = (DPPS+Reserve); 
          Reserve = (money); 
          String end = String.format("%1f", money); 
          t1.setText("$" + end); 
         }else if(counter > DHPDPS && DOTPPS != 0 && DHPDPS != 0){ 
          money = (DOTPPS+Reserve); 
          Reserve = (money); 
          String end = String.format("%1f", money); 
          t1.setText("$" + end); 
         } else{ 

          money = (DPPS+Reserve); 
          Reserve = (money); 
          String end = String.format("%1f", money); 
          t1.setText("$" + end); 
         } 
         counter++; 
         //if(counter == 3000) 
         // t.cancel(); 

         // Display pay per second 
         if(counter <= DHPDPS || DHPDPS == 0){ 
         t2.setText("Your pay per second is: $"+result); 
         }else{ 
          t2.setText("Your pay per second is: $"+result2); 
         } 
        } 
       }); 
      } 
     }, 20, 20); 

    // Make countdown to overtime display 

     final Intent intent1 = new Intent(this, PayTracker.class); 
    // Create the notification 
     final Notification notification = new Notification(R.drawable.ic_launcher, "Click here to check your pay!", System.currentTimeMillis()); 
     // Create an Intent for the notification to launch 
     // Create a PendingIntent for the associated Intent 
     final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent1, 0); 
     cd = new Timer(); 
     final TextView count = (TextView) findViewById(R.id.countdown); 
     convert = (int)HPDPS; 
     cd.schedule(new TimerTask() { 
      @Override 
      public void run() { 
       runOnUiThread(new Runnable() { 
        public void run(){ 
         countdown = (convert - Reserve1); 
         int hours = (countdown/3600); 
         if(OTPPS != 0 && HPDPS != 0){ 
           count.setText("Seconds Remaining to Overtime: " + countdown + "
About " + hours + " Hours"); 
           Reserve1++; 
          } 

         // Set the notification's details 
         final String end = String.format("%.6f", money); 
         notification.setLatestEventInfo(getApplicationContext(), "Your Current Pay:", "$"+end, pendingIntent); 
         // Submit the notification to the system 
         ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, notification); 
        } 
       }); 
      } 

     }, 1000, 1000); 
     // } 
     // } 


     final Button b = (Button) findViewById(R.id.clockout); 
     b.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       if(go == 1) 
        go = 0; 
       if (t != null){ 
        t.cancel(); 
        cd.cancel(); 
       } 
       } 
     }); 

    } 

    public void onRestoreInstanceState(Bundle savedInstanceState) { 
     // Always call the superclass so it can restore the view hierarchy 
     super.onRestoreInstanceState(savedInstanceState); 
      // Restore value of members from saved state 
      countdown = savedInstanceState.getInt(mcountdown); 
      Reserve = savedInstanceState.getInt(mReserve); 
      money = savedInstanceState.getInt(mmoney); 
      counter = savedInstanceState.getInt(mcounter); 
      Reserve1 = savedInstanceState.getInt(mReserve1); 
      HW = savedInstanceState.getInt(mHW); 
      OTW = savedInstanceState.getInt(mOTW); 
      HPD = savedInstanceState.getInt(mHPD); 
      DPPS = savedInstanceState.getInt(mDPPS); 
      go = savedInstanceState.getInt(mgo); 
    } 

    @Override 
    public void onStart(){ 
     super.onStart(); 


    } 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_pay_tracker, menu); 
     return true; 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       NavUtils.navigateUpFromSameTask(this); 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    public void sendMessage(View view) { 
     // Calculate pay per second 
     final double PPS = (HW/3600); 
     DPPS = (PPS/50); 
     final double OTPPS = (OTW/3600); 
     final double DOTPPS = (OTPPS/50); 
     final double HPDPS = (HPD*3600); 
     final double DHPDPS = (HPDPS*50); 
     // Display 

     final TextView t1 = (TextView) findViewById(R.id.yourpay); 
     t1.setTextColor(Color.parseColor("#008000")); 
     final TextView t2 = (TextView) this.findViewById(R.id.payper); 
     final String result2 = String.format("%.8f", OTPPS); 
     final String result = String.format("%.8f", PPS); 

     //if(go != 1){ 
     // go = 1; 
      t.cancel(); 
      cd.cancel(); 
     t = new Timer(); 
     t.schedule(new TimerTask() { 

      @Override 
      public void run() { 
       runOnUiThread(new Runnable() { 

        public void run() { 
         if(DHPDPS==0){ 
          money = (DPPS+Reserve); 
          Reserve = (money); 
          String end = String.format("%1f", money); 
          t1.setText("$" + end); 
         }else if(counter > DHPDPS && DOTPPS != 0 && DHPDPS != 0){ 
          money = (DOTPPS+Reserve); 
          Reserve = (money); 
          String end = String.format("%1f", money); 
          t1.setText("$" + end); 
         } else{ 

          money = (DPPS+Reserve); 
          Reserve = (money); 
          String end = String.format("%1f", money); 
          t1.setText("$" + end); 
         } 
         counter++; 
         if(counter == 3000) 
          t.cancel(); 

         // Display pay per second 
         if(counter <= DHPDPS || DHPDPS == 0){ 
         t2.setText("Your pay per second is: $"+result); 
         }else{ 
          t2.setText("Your pay per second is: $"+result2); 
         } 
        } 
       }); 
      } 
     }, 20, 20); 

    // Make countdown to overtime display 

     final Intent intent1 = new Intent(this, PayTracker.class); 
    // Create the notification 
     final Notification notification = new Notification(R.drawable.ic_launcher, "Click here to check your pay!", System.currentTimeMillis()); 
     // Create an Intent for the notification to launch 
     // Create a PendingIntent for the associated Intent 
     final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent1, 0); 
     cd = new Timer(); 
     final TextView count = (TextView) findViewById(R.id.countdown); 
     convert = (int)HPDPS; 
     cd.schedule(new TimerTask() { 
      @Override 
      public void run() { 
       runOnUiThread(new Runnable() { 
        public void run(){ 
         countdown = (convert - Reserve1); 
         int hours = (countdown/3600); 
         if(OTPPS != 0 && HPDPS != 0){ 
           count.setText("Seconds Remaining to Overtime: " + countdown + "
About " + hours + " Hours"); 
           Reserve1++; 
          } 

         // Set the notification's details 
         final String end = String.format("%.6f", money); 
         notification.setLatestEventInfo(getApplicationContext(), "Your Current Pay:", "$"+end, pendingIntent); 
         // Submit the notification to the system 
         ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, notification); 
        } 
       }); 
      } 

     }, 1000, 1000); 

     //} 
    } 



    @Override 
    public void onSaveInstanceState(Bundle savedInstanceState) { 
     // Save the user's current game state 
     savedInstanceState.putInt(mcountdown, countdown); 
     savedInstanceState.putDouble(mReserve, Reserve); 
     savedInstanceState.putDouble(mmoney, money); 
     savedInstanceState.putDouble(mcounter, counter); 
     savedInstanceState.putDouble(mReserve1, Reserve1); 
     savedInstanceState.putDouble(mHW, HW); 
     savedInstanceState.putDouble(mOTW, OTW); 
     savedInstanceState.putDouble(mHPD, HPD); 
     savedInstanceState.putDouble(mDPPS, DPPS); 
     savedInstanceState.putInt(mgo, go); 

     // Always call the superclass so it can save the view hierarchy state 
     super.onSaveInstanceState(savedInstanceState); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 

     if(t != null) 
      t.cancel(); 
     if(cd != null) 
      cd.cancel(); 
    } 

} 
+0

運行中是否存在while循環? – 2012-08-07 20:23:34

+0

@Andi Jay no theres不是 – Hockeyman271 2012-08-07 20:24:49

+0

你能不能在'onStop()'中調用'Timer.cancel()'? – 2012-08-07 20:25:04

沙发
0
1

這是獲得一種方式在那附近。

static Timer mTimer = null; 

onCreate() { 
    if (mTimer == null) 
     mTimer = new Timer(); 
    } else { 
     // You shouldn't have to do nothing because your timer should be running 
    } 
} 

請注意,這裏一般有幾個問題。靜態基本上只是說創建該對象的一個??實例。作爲副作用,它也試圖回收相同的內存地址。無論哪種方式,一旦你的應用程序在後臺,它可以在任何時候由系統清理,所以你不能保證讓你的定時器回來。還有其他一些方法可以解決這個問題,但這個問題超出了範圍。

+0

您能解釋一些其他方式來解決在後臺有計時器嗎?我知道它不是針對這個問題,但它會幫助我很大程度上與我的項目,即時通訊新的android編程,所以即時通訊仍然試圖學習 – Hockeyman271 2012-08-07 20:50:39

+0

張貼的問題,併發送給我的鏈接 – 2012-08-07 20:51:41

+0

我會盡快我有時間,但我試過這種方法和它的工作原理,唯一的問題是現在我的用戶界面不會在活動重新啓動時更新?我可以在我的通知欄中看到計時器仍在運行,但用戶界面不會填充 – Hockeyman271 2012-08-07 20:59:33

0
votes
answers
21 views
+10

如何使用基於註解的配置來代替基於XML的配置

0

我通過基於XML的配置將Spring框架用於依賴注入。如何使用基於註解的配置來代替基於XML的配置

例如,我有3類:

1. public class Robot implements IRobot{ 

     IHand hand; 

     -//- 

    } 

    2. public class SonyHand implements IHand{ -//- } 

    3. public class ToshibaHand implements IHand{ -//- } 

而且我在XML文件:

<beans ...> 

    <bean id="robot1" class="somepackage.Robot.class"> 
    <property name="hand" ref="sonyHand"> 
    </bean> 

    <bean id="robot2" class="somepackage.Robot.class"> 
    <property name="hand" ref="toshibaHand"> 
    </bean> 

    <bean id="sonyHand" class="somepackage.SonyHand.class"/> 


    <bean id="toshibaHand" class="somepackage.ToshibaHand.class"/>  

</beans> 

因此,春天的IoC(容器)將創建四個豆(對象)中所有。

Bean robot1(將引入一個sonyHand bean)。

Bean robot2(將引入一個toshibaHand bean)。

豆sonyHand。

豆toshibaHand。

問題是,我可以做純粹基於註釋的配置完全相同的東西嗎?如果我這樣做,那麼如何?我已經試過這樣:

@Configuration 
public class AppConfig{ 

    @Bean 
    public Robot robot1(){ 
    return new Robot(); 
    } 

    @Bean 
    public Robot robot2(){ 
    return new Robot(); 
    } 

    @Bean 
    @Qualifier("sonyH") 
    public SonyHand sonyHand(){ 
    return new SonyHand(); 
    } 

    @Bean 
    @Qualifier("toshibaH") 
    public ToshibaHand toshibaHand(){ 
    return new ToshibaHand(); 
    } 
} 

輕微改變類:

1. public class Robot implements IRobot{ 

     @Autowired("sonyH") 
     IHand hand; 

     -//- 

    } 

    2. public class SonyHand implements IHand{ -//- } 

    3. public class ToshibaHand implements IHand{ -//- } 

這裏幾乎一無所有的XML文件中:

<beans ...> 

    <context:component-scan base-package="somepackage"/>  

</beans> 

這一切工作,但這不是我需要什麼,因爲容器創建的豆將與前面的示例稍有不同:

Bean機器人1(將引入sonyHand bean)。

Bean機器人2(同樣會引入sonyHand bean)。

豆sonyHand。

豆toshibaHand。

我知道爲什麼會發生這種情況(因爲@Autowired(「sonyH」)),但我不知道如何解決它的問題,就像使用基於XML的配置一樣。

沙发
0
1

稍微重構類

@Configuration 
public class AppConfig{ 

    @Bean 
    public Robot robot1(IHand sonyH){ 
    return new Robot(sonyH); 
    } 

    @Bean 
    public Robot robot2(IHand toshibaH){ 
    return new Robot(toshibaH); 
    } 

    @Bean(name = "sonyH") 
    public SonyHand sonyHand(){ 
    return new SonyHand(); 
    } 

    @Bean(name = "toshibaH") 
    public ToshibaHand toshibaHand(){ 
    return new ToshibaHand(); 
    } 
} 

現在,如果你嘗試自動裝配這樣

@Autowired 
Robot robot1 // this will have sonyH instance 

@Autowired 
Robot robot2 // this will have toshibaH instance 
+0

我很感激你。這解決了我的問題,並最終使我對這個主題的理解變得清晰。 –

+0

很高興幫助:) – pvpkiran

0
votes
answers
25 views
+10

如何使用Java API執行Presto查詢?

-1

我在AzureQubole Data Service中使用Presto。我想從Java程序執行Presto查詢。我如何在Java程序的Azure上的Qubole數據服務上的Presto集羣中執行查詢?如何使用Java API執行Presto查詢?

+0

你到目前爲止嘗試過什麼? – vektor

沙发
0
2

Presto提供了一個正常的JDBC驅動程序,允許您運行SQL查詢。所有你需要做的就是把它包含在你的Java應用程序中。對於如何連接到普雷斯托集羣在其網站上https://prestodb.io/docs/current/installation/jdbc.html一個例子:

// URL parameters 
String url = "jdbc:presto://example.net:8080/hive/sales"; 
Properties properties = new Properties(); 
properties.setProperty("user", "test"); 
properties.setProperty("password", "secret"); 
properties.setProperty("SSL", "true"); 
Connection connection = DriverManager.getConnection(url, properties); 

// properties 
String url = "jdbc:presto://example.net:8080/hive/sales?user=test&password=secret&SSL=true"; 
Connection connection = DriverManager.getConnection(url); 

我希望你知道如何使用Java中的正常數據庫執行SQL語句。如果不是看到https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html

從本質上講,

Statement stmt = null; 
String query = "select COF_NAME, SUP_ID, PRICE, " + 
       "SALES, TOTAL " + 
       "from " + dbName + ".COFFEES"; 
try { 
    stmt = con.createStatement(); 
    ResultSet rs = stmt.executeQuery(query); 
    while (rs.next()) { 
     String coffeeName = rs.getString("COF_NAME"); 
     int supplierID = rs.getInt("SUP_ID"); 
     float price = rs.getFloat("PRICE"); 
     int sales = rs.getInt("SALES"); 
     int total = rs.getInt("TOTAL"); 
     System.out.println(coffeeName + "	" + supplierID + 
          "	" + price + "	" + sales + 
          "	" + total); 
    } 
} catch (SQLException e) { 
    JDBCTutorialUtilities.printSQLException(e); 
} finally { 
    if (stmt != null) { stmt.close(); } 
} 

至於搞清楚正確的連接參數(在第一個例子JDBC URL)爲您的環境,請參考第Qubole你友好的技術支持。

0
votes
answers
41 views
+10

access_token在嘗試使用ADAL.js AuthenticationContext獲取訪問標記時與id_token相同?

4

我使用Azure AD對我的單頁應用程序(Angular4)進行身份驗證,並使用Adal.js進行驗證。在登錄頁面上,我單擊一個重定向到Microsoft AAD的按鈕,並在成功登錄後重定向迴應用程序主頁,並從JWT收到id_token和用戶信息。access_token在嘗試使用ADAL.js AuthenticationContext獲取訪問標記時與id_token相同?

我需要access_token用於後端API訪問,而我試圖通過該ADAL AuthenticationContextgetCachedToken()方法來獲取併發送的clientId作爲參數:

this.context.getCachedToken(this.configService.AdalConfig.clientId) 

但這種方法會返回存儲在會話存儲中的相同標記爲id_token (adal.idtoken)adal.access_token.key239f6fc7-64d2-3t04-8gfd-501efc25adkd = <id-token-value>:它基本上與級聯鍵,它具有相同的值,id_token

adal.access_token.key + clientId = id_token 

前創建會話存儲的新項目。

我也嘗試用AuthenticationContext.acquireToken()方法取access_token方法,但它也給了id_token回來。

我哪裏錯了?

編輯:張貼代碼。 我在調用函數login(),併成功登錄後,試圖通過 adal.config.ts中的get accessToken()屬性訪問器獲取主頁中的訪問令牌。

config.service.ts

import { Injectable } from '@angular/core'; 

@Injectable() 
export class ConfigService { 
    constructor() {} 
    public get AdalConfig(): any { 
    return { 
     tenant: 'common', 
     clientId: <application-id>, 
     redirectUri: window.location.origin + '/', 
     postLogoutRedirectUri: window.location.origin + '/' 
    }; 
    } 
} 

adal.service.ts

import { ConfigService } from './config.service'; 
import { Injectable } from '@angular/core'; 
import { adal } from 'adal-angular'; 
let createAuthContextFn: adal.AuthenticationContextStatic = AuthenticationContext; 

@Injectable() 
export class AdalService { 
    private context: adal.AuthenticationContext; 
    constructor(private configService: ConfigService) { 
    this.context = new createAuthContextFn(configService.AdalConfig); 
    } 

    login() { 
    this.context.login(); 
    } 

    logout() { 
    this.context.logOut(); 
    } 

    handleCallback() { 
    this.context.handleWindowCallback(); 
    } 

    public get userInfo() { 
    return this.context.getCachedUser(); 
    } 

    public get accessToken() { 
    return this.context.getCachedToken(this.configService.AdalConfig.clientId); 
    // return this.context.acquireToken(this.configService.AdalConfig.clientId, function(message, token, response) { 
    // console.log(message, token, response); 
    // }); 
    } 

    public get isAuthenticated() { 
    return this.userInfo && this.accessToken; 
    } 
} 
+0

你應該張貼您的整個驗證碼... –

+0

爲了驗證我只是創造了所有必要的信息的'AdalConfig'對象,如'tenant','clientId','redirectUri'等,然後初始化一個使用'AdalConfig'的新'AuthenticationContext',然後使用初始化上下文的方法。我發佈了方法調用。請讓我知道還有什麼是必需的。 – Rishabh

+0

你在哪裏指定你想要撥打的資源?你需要發佈你的代碼或者沒有人能夠幫助你。 –

沙发
0
2

其實一點閱讀後,原來那SPA的連接到Azure的指令要求的OAuth 2.0隱式授予流程。該Microsoft documentation說:

在這種情況下,當,JavaScript的前端 使用Active Directory身份驗證JavaScript庫(ADAL.JS)用戶登錄 和隱含授權授權以獲得一個ID令牌(id_token )來自Azure AD的 。當調用其Web API後端 (使用OWIN中間件進行保護)時,令牌被緩存,客戶端將其作爲不記名令牌附加到 請求中。

因此,我需要將id_token本身發送到後端API,後者又可以進行驗證和使用。有關驗證的更多信息,請參閱here

只接收到id_token不足以驗證用戶; 您必須驗證id_token的簽名並根據您的應用程序的要求驗證 令牌中的聲明。 v2.0端點使用JSON Web 令牌(JWT)和公鑰密碼體系對令牌進行簽名,並驗證 它們是否有效。

您可以選擇驗證客戶端 代碼中的id_token,但通常的做法是將id_token發送到後端 服務器並在其中執行驗證。一旦您驗證了id_token的 簽名,就會有一些聲明要求您驗證 。

+0

你是否設法使用adal從id令牌獲取訪問令牌? – hngdev

0
votes
answers
11 views
+10

如何在應用變換比例後檢索div的實際大小?

0

我將transform: scale(n)應用於div後,我通過javascript獲得的widthheight保持不變。如何在應用變換比例後檢索div的實際大小?

下面舉例說明設置。

console.log($('.parent1').width()) 
 
console.log('parent1 w ' + $('.parent1 .container').width()) 
 
console.log('parent1 h ' + $('.parent1 .container').height()) 
 

 
$('.parent2').css('transform', 'scale(1.5)') 
 
console.log('parent2 w ' + $('.parent2 .container').width()) 
 
console.log('parent2 h ' + $('.parent2 .container').height())
.parent { 
 
    width: 250px; 
 
} 
 

 
.parent1 { 
 
    height: 100px; 
 
} 
 

 
.parent2 { 
 
    height: 100px; 
 
} 
 

 
.container { 
 
    display: block; 
 
    height:50%; 
 
    width:50%; 
 
    margin:auto; 
 
    border:solid 1px red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="parent parent1"> 
 
    <div class="container"> 
 
    </div> 
 
</div> 
 

 
<div class="parent parent2"> 
 
    <div class="container"> 
 
    </div> 
 
</div>

正如可以從控制檯中,第二容器的寬度和高度打印出看到的是一樣的第一個。但是對於用戶而言,第二個容器實際上更大。

如何獲得$('.parent2 .container')的實際尺寸?

從另一個過程中應用比例尺,我無法獲取比例因子。

+2

乘大小與規模有多大? –

+0

比例因子不適用於我。 –

+1

你可以明顯地使用'element.getBoundingClientRect()'(在這裏找到:https://stackoverflow.com/questions/5603615/get-the-scale-value-of-an-element) –

沙发
0
1

使用getBoundingClientRect()

console.log($('.parent1').width()) 
 
console.log('parent1 w ' + $('.parent1 .container').width()) 
 
console.log('parent1 h ' + $('.parent1 .container').height()) 
 

 
$('.parent2').css('transform', 'scale(1.5)') 
 
console.log('parent2 w ' + $(".parent2 .container")[0].getBoundingClientRect().width) 
 
console.log('parent2 w ' + $(".parent2 .container")[0].getBoundingClientRect().height)
.parent { 
 
    width: 250px; 
 
} 
 

 
.parent1 { 
 
    height: 100px; 
 
} 
 

 
.parent2 { 
 
    height: 100px; 
 
} 
 

 
.container { 
 
    display: block; 
 
    height:50%; 
 
    width:50%; 
 
    margin:auto; 
 
    border:solid 1px red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="parent parent1"> 
 
    <div class="container"> 
 
    </div> 
 
</div> 
 

 
<div class="parent parent2"> 
 
    <div class="container"> 
 
    </div> 
 
</div>

+0

爲什麼縮放的寬度/高度不是原始寬度/高度的1.5倍? –

+0

@AnonyKong回答更新,現在檢查 – Akasa

0
votes
answers
24 views
+10

使用掃描儀需要幫助

-2

所以我正在做這件事,要求你輸入你想要的數字,然後讓你輸入一個數字。該代碼應該檢查這個數字是否是第一次提交的數字之一,如果是,會彈出一條消息說出來,然後退出該程序。有人可以幫助我如何正確使用掃描儀,或者如果這不是我的代碼中的問題,幫我糾正我的代碼?使用掃描儀需要幫助

package individuellt_val; 

    import java.util.Scanner; 
    import javax.swing.JOptionPane; 


    public class InlKap9test2 { 

     public static void main(String[] args) { 
      String s = JOptionPane.showInputDialog("Ange några tal"); 
      Scanner sc = new Scanner(s); 
      int x = 0, y = 0; 
      boolean fortsätt = true; 

      while(fortsätt){ 
       while(sc.hasNextInt()){ 
        x = sc.nextInt(); 

       String s2 = JOptionPane.showInputDialog("Ange ett tal till"); 
       Scanner sc2 = new Scanner(s2); 
       while(sc2.hasNextInt()) 
       y = sc.nextInt(); 

       if(x == y){ 
        JOptionPane.showMessageDialog(null, "Detta tal fanns bland de första!"); 
        fortsätt = false; 
       } 
       } 
      } 
    System.exit(0); 
     } 

    } 
+1

1)*「我似乎無法讓它工作。」*你能設法提出一個問題嗎? 2)請使用代碼格式設置代碼和代碼片段,結構化文檔(如HTML/XML或輸入/輸出)。爲此,請選擇文本並單擊郵件發佈/編輯表單頂部的「{}」按鈕。 –

+0

使用'新的掃描儀(System.in)' –

沙发
0
1

說明

new Scanner(s)不正確。 s是一個字符串,而Scanner需要PrintStream。你可能想從鍵盤上找到它,又名System.in

如何解決

變化Scanner sc = new Scanner(s);Scanner sc = new Scanner(System.in)

板凳
0
0

String s = JOptionPane.showInputDialog(「Angenågratal」); 掃描儀sc =新掃描儀

在這裏你不能通過掃描儀的參考。 使用 new Scanner(System.in);

它現在將從控制檯,即鍵盤輸入輸入。

19
votes
answers
26 views
+10

Using context in a fragment

How can I get the context in a fragment?

I need to use my database whose constructor takes in the context, but getApplicationContext() and FragmentClass.this don't work so what can I do?

Database constructor

public Database(Context ctx)
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}
沙发
+50

to get the context inside the Fragment will be possible using getActivity() :

public Database()
{
    this.context = getActivity();
    DBHelper = new DatabaseHelper(this.context);
}
  • Be careful, to get the Activity associated with the fragment using getActivity(), you can use it but is not recommended it will cause memory leaks.

I think a better aproach must be getting the Activity from the onAttach() method:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    context = activity;
}
板凳
+40

You could also get the context from the inflater parameter, when overriding onCreateView.

public static class MyFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
        /* ... */
        Context context = inflater.getContext();
        /* ... */
    }
}
地板
+40

Another alternative approach is:

You can get the context using:

getActivity().getApplicationContext();
4楼
+30

getContext() came in API 23. Replace it with getActivity() everywhere in the code.

See if it fixes the error. Try to use methods which are in between the target and minimun API level, else this error will come in place.

5楼
+30

Since API level 23 there is getContext() but if you want to support older versions you can use getActivity().getApplicationContext() while I still recommend using the support version of Fragment which is android.support.v4.app.Fragment.