在Spring MVC项目中使用静态变量来保存文件属性是一种很好的做法(Is a good practice to use static variables for holding properties from file in a Spring MVC project)
下面的代码工作正常但我怀疑我是否使用最好的方法来处理需求:从一些地方读取Spring项目中属性文件的属性。 基本上,我创建了一个带有静态变量的公共类。 现在我使用Spring 3加JDK6。 我可能很快将Spring升级到4,但我无法使用JDK版本。
MVC-调度-servlet.xml中
属性文件
url = http://localhost:8080/MHE2/ lastpage = log/display/last firstpage = log/display/first previouspage = log/display/previous nextpage = log/display/next
一个使用属性值的示例。 会有很多像这样的案例
//calling rest web service DefaultHttpClient httpClient = new DefaultHttpClient(); //Apache HttpClient HttpPost postRequest = new HttpPost(Ut_Properties.getUrl() + Ut_Properties.getLastpage());
带有静态变量的公共类
package com.mastercard.mhe.utilities; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component("ut_Properties") @Scope("singleton") public class Ut_Properties { private static String url; private static String lastpage; private static String firstpage; private static String previouspage; private static String nextpage; @Autowired public Ut_Properties(@Value("${url}") String url, @Value("${lastpage}") String lastpage, @Value("${url}") String previouspage, @Value("${lastpage}") String nextpage) { Ut_Properties.setUrl(url); Ut_Properties.setLastpage(lastpage); Ut_Properties.setUrl(previouspage); Ut_Properties.setLastpage(nextpage); } public static String getUrl() { return url; } public static void setUrl(String url) { Ut_Properties.url = url; } public static String getFirstpage() { return firstpage; } public static void setFirstpage(String firstpage) { Ut_Properties.firstpage = firstpage; } public static String getPreviouspage() { return previouspage; } public static void setPreviouspage(String previouspage) { Ut_Properties.previouspage = previouspage; } public static String getNextpage() { return nextpage; } public static void setNextpage(String nextpage) { Ut_Properties.nextpage = nextpage; } public static String getLastpage() { return lastpage; } public static void setLastpage(String lastpage) { Ut_Properties.lastpage = lastpage; } }
The code below is working perfectly but I am in doubt if I am using the best approach for the requirenment: read, from a couple of places, property from a properties file in a Spring project. Basically, I created a public class with static variables. Now I am using Spring 3 plus JDK6. I might upgrade Spring to 4 soon but I will not be able to chnage JDK version.
mvc-dispatcher-servlet.xml
Properties File
url = http://localhost:8080/MHE2/ lastpage = log/display/last firstpage = log/display/first previouspage = log/display/previous nextpage = log/display/next
One sample of using the property value. There will be a lot of case like these
//calling rest web service DefaultHttpClient httpClient = new DefaultHttpClient(); //Apache HttpClient HttpPost postRequest = new HttpPost(Ut_Properties.getUrl() + Ut_Properties.getLastpage());
Public class with static variables
package com.mastercard.mhe.utilities; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component("ut_Properties") @Scope("singleton") public class Ut_Properties { private static String url; private static String lastpage; private static String firstpage; private static String previouspage; private static String nextpage; @Autowired public Ut_Properties(@Value("${url}") String url, @Value("${lastpage}") String lastpage, @Value("${url}") String previouspage, @Value("${lastpage}") String nextpage) { Ut_Properties.setUrl(url); Ut_Properties.setLastpage(lastpage); Ut_Properties.setUrl(previouspage); Ut_Properties.setLastpage(nextpage); } public static String getUrl() { return url; } public static void setUrl(String url) { Ut_Properties.url = url; } public static String getFirstpage() { return firstpage; } public static void setFirstpage(String firstpage) { Ut_Properties.firstpage = firstpage; } public static String getPreviouspage() { return previouspage; } public static void setPreviouspage(String previouspage) { Ut_Properties.previouspage = previouspage; } public static String getNextpage() { return nextpage; } public static void setNextpage(String nextpage) { Ut_Properties.nextpage = nextpage; } public static String getLastpage() { return lastpage; } public static void setLastpage(String lastpage) { Ut_Properties.lastpage = lastpage; } }
原文:https://stackoverflow.com/questions/27281172
最满意答案
尝试这个: -
m_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (mAdapter.getLink(position) == null) {} else { String linked = mAdapter.getLink(position); Uri url = Uri.parse("https://"+linked); Intent intent = new Intent(Intent.ACTION_VIEW, url); startActivity(intent); } } }); }
try to this:-
m_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (mAdapter.getLink(position) == null) {} else { String linked = mAdapter.getLink(position); Uri url = Uri.parse("https://"+linked); Intent intent = new Intent(Intent.ACTION_VIEW, url); startActivity(intent); } } }); }
相关问答
更多-
但问题是在播放音频时如果有人点击其他活动声音不会停止 在WordAdapter创建一个getter方法,它返回当前播放的MediaPlayer实例,并在开始下一个Activity之前在onItemClick调用它,如: public MediaPlayer getMPlayerInstace(){ return this.mediaPlayer; } 现在在onItemClick调用stop方法: MediaPlayer mPlayer; mPlayer=adapter.getMPlayerIns ...
-
尝试这个: - m_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { if (mAdapter.getLink(position) == null) {} ...
-
更新数据后,只需在click事件中调用notifyDataSetChanged() 。 upvote.setOnClickListener( //in here new View.OnClickListener(){ public void onClick(View v){ comments.get(position)[1] = Integer.toString(Integer.parseInt(comments ...
-
调试getImageId(m_context, "c" + m_list.get(position).name.toLowerCase()返回的int getImageId(m_context, "c" + m_list.get(position).name.toLowerCase() 它可能返回0或-1,它不会在您的imageview中设置图像并且视图会折叠! debug the int returned by getImageId(m_context, "c" + m_list.get(position ...
-
您需要在适配器中维护所选项目并使用它来更改文本: 适配器代码 private int selectedIndex; @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder holder; LayoutInflater inflater = ((Activity) activity).ge ...
-
我刚刚解决了这个问题,通过使用setOnItemClickListener和setOnItemLongClickListener与ListView适配器.. listView.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView> arg0, View arg1, int arg2, long a ...
-
使用扩展BaseAdapter的自定义适配器刷新ListView(Refresh ListView with custom adapter that extends BaseAdapter)[2024-02-21]
您正在使用.addAll()将数据附加到适配器已有的数据。 快速修复是在使用.clear()调用.addAll()之前删除所有数据。 //... this.businesses.clear(); this.businesses.addAll(businesses); adapter.notifyDataSetChanged(); //... You are using .addAll() which appends data to the data the adapter already has. The ... -
移动setadapter ArrayList
FullMenu; FullMenu = mydb.getPlates("Entrees"); Plate p; int i; MyClassAdapter adapter = new MyClassAdapter(this,0,FullMenu); ListView listView = (ListView) findViewById(R.id.list); for (i=0; i < FullMe ... -
如何为具有额外数据的自定义数组适配器设置onClick(How to set onClick for custom array adapter with extra data)[2021-08-14]
用你的适配器getview更新 row.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri ... -
自定义ListView适配器上的Android onclick事件无效(Android onclick Event on custom ListView adapter not working)[2022-10-09]
我猜你的Adapter包含Person类型的对象。 由于Person类中的字段是公共的,因此您可以像这样访问它们。 @Override public void onItemClick(AdapterView> arg0, View arg1, int position, long arg3) { // Get Person "behind" the clicked item Person p = (Person) myListView.getItemAtPosition(positio ...