1、使用jquery動態(tài)設(shè)置下拉框selected
需求就是根據(jù)下拉框的值動態(tài)的設(shè)置為selected,本以為很簡單,網(wǎng)上一大推的方法,挨著嘗試了之后卻發(fā)現(xiàn)沒有一個是有用的。網(wǎng)上的做法如下:
<select id="selectID ">
<option>選擇A</option>
<option>選擇B</option>
<option>選擇C</option>
</select>
// 方法一:
$("#selectID option[text='選擇B']").attr("selected", "selected");
// 方法二:
$("#selectID ").find("option[text='選擇B']").attr("selected",true);
// 方法三:也有人說高版本的jquery應(yīng)該寫成下面的樣子
$("#selectID option[text='選擇B']").prop("selected", true);
不管是用什么方法都不起作用,繼續(xù)查找更多資料后上面這些方法在jquery低于1.4.2的版本(含)中有效,在更高版本中無效?。?!
注意?。。∩厦娴姆椒ň黄鹱饔?,有效的方法如下:
解決一:精確匹配,選擇文本與所給字符串完全一樣的option。
$('#selectID option').filter(function(){return $(this).text()=="選擇B";}).attr("selected",true);
解決二:子串匹配,選擇文本包含所給字符串的option。
$("#selectID option:contains('選擇B')").attr('selected', true);
2、struts2的action中的方法重復(fù)執(zhí)行的原因
struts2中使用json插件(struts2-json-plugin)執(zhí)行ajax處理時,如果方法名是get方法的時候,方法會莫名其妙的執(zhí)行兩次。
各種debug都找出原因在哪里,差點(diǎn)以為自己寫的代碼中邪了。又是繼續(xù)百度之后,找到的問題的原因
原因:struts2 中JSON的原理是在ACTION中的get方法都會序列化,前面是get的方法只要沒指定不序列化,都會在序列化時再執(zhí)行一次。
解決方法:
1、Action中的業(yè)務(wù)方法前不要以get開頭 (屬性的get set 除外)
2、用@JSON(serialize=false)指定方法不序列化 (此辦法沒有親自實(shí)現(xiàn),僅供參考)
沒有嘗試添加注解的方式解決問題(因?yàn)楦姆椒奖?,并且get開頭的方法名也不規(guī)范),所以以后在給方法起名字的時候,還是要十分注意,不要造成不必要的麻煩。
3、獲取本機(jī)的ip地址(排除虛擬機(jī)等ip)
又是在網(wǎng)上查找資料遇到很多坑,很多Java獲取本機(jī)ip地址的方法要么是根本獲取不到,要么是獲取的有問題。
網(wǎng)上常見的方法如下
InetAddress.getLocalHost().getHostAddress()
但是如果電腦里面有Lan,WIFI,藍(lán)牙熱點(diǎn),虛擬機(jī)網(wǎng)卡,即存在很多的網(wǎng)絡(luò)接口(network interfaces),每個網(wǎng)絡(luò)接口就包含一個IP地址,并不是所有的IP地址能被外部或局域網(wǎng)訪問,比如說虛擬機(jī)網(wǎng)卡地址等等。上面獲取到的ip就會有誤。
下面是正確的獲取ip地址的方法,親測無誤:
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class getRealIp {
public static void main(String[] args) {
try {
// 正確的IP拿法
System.out.println("get LocalHost LAN Address : " + getLocalHostLANAddress().getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
// 正確的IP拿法,即優(yōu)先拿site-local地址
private static InetAddress getLocalHostLANAddress() throws UnknownHostException {
try {
InetAddress candidateAddress = null;
// 遍歷所有的網(wǎng)絡(luò)接口
for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
// 在所有的接口下再遍歷IP
for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
if (!inetAddr.isLoopbackAddress()) {// 排除loopback類型地址
if (inetAddr.isSiteLocalAddress()) {
// 如果是site-local地址,就是它了
return inetAddr;
} else if (candidateAddress == null) {
// site-local類型的地址未被發(fā)現(xiàn),先記錄候選地址
candidateAddress = inetAddr;
}
}
}
}
if (candidateAddress != null) {
return candidateAddress;
}
// 如果沒有發(fā)現(xiàn) non-loopback地址.只能用最次選的方案
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
if (jdkSuppliedAddress == null) {
throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
}
return jdkSuppliedAddress;
} catch (Exception e) {
UnknownHostException unknownHostException = new UnknownHostException(
"Failed to determine LAN address: " + e);
unknownHostException.initCause(e);
throw unknownHostException;
}
}
}
參考博客地址:https://blog.csdn.net/u011809209/article/details/77236602
這里插上一句,上面的方法之前在java項(xiàng)目中能準(zhǔn)確獲取到ip,但是放在項(xiàng)目中之后,又不能獲取準(zhǔn)確的ip,也是很迷,然后又另外找了一種方法,如下:
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
/**
* 獲取本地真正的IP地址,即獲得有線或者無線WiFi地址。
* 過濾虛擬機(jī)、藍(lán)牙等地址
*/
public class getRealLocalIP {
public static String getRealIP() {
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface
.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
.nextElement();
// 去除回環(huán)接口,子接口,未運(yùn)行和接口
if (netInterface.isLoopback() || netInterface.isVirtual()
|| !netInterface.isUp()) {
continue;
}
if (!netInterface.getDisplayName().contains("Intel")
&& !netInterface.getDisplayName().contains("Realtek")) {
continue;
}
Enumeration<InetAddress> addresses = netInterface
.getInetAddresses();
System.out.println(netInterface.getDisplayName());
while (addresses.hasMoreElements()) {
InetAddress ip = addresses.nextElement();
if (ip != null) {
// ipv4
if (ip instanceof Inet4Address) {
System.out.println("ipv4 = " + ip.getHostAddress());
return ip.getHostAddress();
}
}
}
break;
}
} catch (SocketException e) {
System.err.println("Error when getting host ip address"
+ e.getMessage());
}
return null;
}
}
參考博客地址:https://blog.csdn.net/yinshuomail/article/details/81624648
4、獲得userAgent(用戶代理)的方法
通過userAgent可以判斷用戶當(dāng)前操作的是桌面端設(shè)備還是移動設(shè)備,可以根據(jù)不同的設(shè)備進(jìn)行適配。
js獲取的方法:
var userAgent = navigator.userAgent
java后臺寫法:request為HttpServletRequest
String userAgent = request.getHeader("User-Agent");
5、css實(shí)現(xiàn)兩端對齊的3種方法
本人是一個css渣,就不在這里班門弄斧了,給個傳送門,可以參考這位大神的講解,文末也有移動端文本兩端對齊示例,可以說是非常好的學(xué)習(xí)資料了。
傳送門:css實(shí)現(xiàn)兩端對齊的3種方法