public class Main
{
public static void main(String []ar)
{
A m = new A();
System.out.println(m.getNull().getValue());
}
}
class A
{
A getNull()
{
return null;
}
static int getValue()
{
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
我在SCJP书中遇到过这个问题.代码打印出来1而不是预期的NPE.有人可以解释相同的原因吗?
有没有什么办法可以使用正则表达式将字符串格式化为特定模式,或者是stringbuilder + substring更快的方法?
例如,输入电话号码 - > 1234567890
输出为 - >(123)456-7890
我看到这篇文章是可能的:http://www.4guysfromrolla.com/webtech/031302-1.shtml但是给出的解释是在ASP中.我怎么用Java做的???
如果我写这个脚本:
alert(parseInt("123blahblahblah456"));
我得到了值的警报 123
理想情况下,函数不应该做任何事情,因为它是一个无效的整数字符串?情况类似parseFloat()
我有基于JSF的页面的以下主模板文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title><ui:insert name="title">MyApp</ui:insert></title>
<h:outputStylesheet name="stylesheet.css" library="styles"/>
</h:head>
<h:body>
<div id="container">
<div id="header">
<ui:insert name="header">
// header content
</ui:insert>
</div>
<div id="content">
<ui:insert name="content">
</ui:insert>
</div>
<div id="footer">
<ui:insert name="footer">
</ui:insert>
</div>
</div>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
在头部,我们有stylesheet.css.此样式表包含所有页面共有的全局样式.
在模板客户端中,我想添加页面特定的样式表.所以,我尝试在我的模板客户端页面中添加以下行:
<ui:composition template="/pages/templates/template.xhtml">
<ui:define name="content">
<h:outputStylesheet name="indexPage.css" library="styles" target="head"/>
// body content
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
但是,这似乎没有添加indexPage.css到生成的HTML头部.
我正在使用Mojarra 2.1.2.它是否支持该target属性?我没有看到它被列为Eclipse中我的autosuggest选项中的可用选项之一.
如果没有,如何在仍使用模板的同时注入额外的页面特定CSS?
我正在尝试编写一个方法,它将返回与我需要传递给Web服务的银行产品相对应的代码.我有一系列符合条件的通用类型的产品,输入将是一个字符串,它将是数组中任何泛型类型的特定类型.让我通过我已有的代码解释一下:
public static void main(String[] args)
{
String[] names = { "Checking", "Savings", "DEMAT", "DEMAT Savings", "Interest Checking" };
String input = "Employee Checking";
int min = Integer.MAX_VALUE;
String maxMatch = null;
for(String name : names)
{
int i = input.indexOf(name);
if(i > -1 && i < min)
{
min = i;
maxMatch = name;
}
}
if(null != maxMatch)
{
System.out.println("Maximum match for " + input + " found at " + maxMatch);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码片段尝试为输入执行最大匹配.因此,如果我将"员工兴趣检查"作为输入,我会在"兴趣检查"中获得匹配而不仅仅是"正在检查".
我想知道的是,是否有任何方法可以进一步优化此代码段,或者是否存在此代码失败的情况?
我的问题与这个问题类似。我有一个BaseBean,目前只有一个属性,该属性被注释为@ManagedProperty.
但是,当我在命令按钮的操作方法中访问此继承的托管属性的 getter 时,它返回 null。我调试并确认基本 bean 构造函数被调用了两次 - 一次在页面加载时调用,下一步在单击按钮时调用,如上述链接中所述。
我遵循了文章选择的答案以及这篇文章中提到的建议,但无济于事。
以下是我的代码:
public abstract class BaseBean
{
@ManagedProperty(value = "#{serviceLocator}")
private IServiceLocator serviceLocator;
public IServiceLocator getServiceLocator() {
return serviceLocator;
}
public void setServiceLocator(IServiceLocator serviceLocator) {
this.serviceLocator = serviceLocator;
}
}
Run Code Online (Sandbox Code Playgroud)
@ManagedBean
@ViewScoped
public class RegistrationBean extends BaseBean implements Serializable
{
private static final long serialVersionUID = -6449858513581500971L;
private String userID;
private String password;
private String firstName;
private String lastName;
private String email; …Run Code Online (Sandbox Code Playgroud) 我在使用时看到这种奇怪的行为@ManagedProperty.我有2个豆子:
UserManager(SessionScoped)
@ManagedBean
@SessionScoped
public class UserManager extends BaseBean implements Serializable
{
private static final long serialVersionUID = 1861000957282002416L;
private User currentUser;
public String login()
{
// set value of currentUser after authentication
}
public User getCurrentUser() {
return currentUser;
}
public boolean isLoggedIn() {
return getCurrentUser() != null;
}
}
Run Code Online (Sandbox Code Playgroud)
CartBean(ALSO SessionScoped)
...
import javax.faces.bean.ManagedProperty;
...
@ManagedBean
@SessionScoped
public class CartBean extends BaseBean implements Serializable
{
@ManagedProperty(value = "#{userManager.loggedIn}")
private boolean loggedIn;
public void …Run Code Online (Sandbox Code Playgroud) 我在eclipse(JUNO)中使用JSF2.1功能创建了一个动态Web项目,我按照下面链接中的所有步骤,http: //help.eclipse.org/helios/index.jsp?topic =%2Forg.eclipse.jst .jsf.doc.user%2Fhtml%2Fgettingstarted%2Ftutorial%2FJSFTools_tutorial_JSF20.html
我的问题是faces-config文件只有XML格式,但我没有得到"简介,概述,导航规则,托管bean,组件,其他和源"选项卡.在faces.config.xml文件中..
我怎样才能获得标签?
jsf-2 ×4
java ×3
jsf ×2
string ×2
eclipse ×1
el ×1
inheritance ×1
javascript ×1
optimization ×1
regex ×1