我究竟做错了什么?
问题是我将如何包含一个equals()方法,Participant如果它们在所有三个字段中具有相同的值,则确定两个s相等?包括为每个字段分配参数值的构造函数以及toString()返回包含所有值的String 的方法.
import javax.swing.JOptionPane;
import java.util.*;
public class Abc {
private static Ab mini[] = new Ab[2];
private static Ab diving[] = new Ab[2];
public static void main(String[] args) {
String name = "";
String add = "";
int age = 0;
Ab p = new Ab(name, age, add);
Ab p1 = new Ab(name, age, add);
setParticipant();
setParticipant1();
displayDetail();
displayDetail1();
if (p == p1) {
System.out.println("equal");
} else {
System.out.println("not equal");
}
} …Run Code Online (Sandbox Code Playgroud) 为了获得当前日期,我在faces-config.xml下面声明了一个托管bean 。
<managed-bean>
<managed-bean-name>currentDate</managed-bean-name>
<managed-bean-class>java.time.ZonedDateTime</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
Run Code Online (Sandbox Code Playgroud)
之前,我一直在使用org.joda.time.DateTime并且工作正常。可以像下面这样在EL中访问此bean。
<h:outputText value="#{currentDate}" converter="#{converter}"/>
Run Code Online (Sandbox Code Playgroud)
但是,这导致引发以下异常。
16:15:29,984 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (default task-16) Error Rendering View[/admin_side/Home.xhtml]: com.sun.faces.mgbean.ManagedBeanCreationException: Unable to create managed bean currentDate. The following problems were found:
- Managed bean class java.time.ZonedDateTime for managed bean currentDate doesnt declare a public no-argument constructor.
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:265)
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:257)
at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:117)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:116)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIOutput.getValue(UIOutput.java:174)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
at …Run Code Online (Sandbox Code Playgroud) 是否可以明确拒绝 JSF 序列化某些组件树?目前我正在将一个不可序列化的对象传递给<h:inputText>:
<h:inputText value="#{nonSerializableBean.nonSerializableClassInstance}" />
Run Code Online (Sandbox Code Playgroud)
单击几次后会发生什么(在视图恢复期间):
javax.faces.FacesException: Unexpected error restoring state for component
with id configurationForm:j_idt292:j_idt302:field. Cause:
java.lang.IllegalStateException: java.lang.InstantiationException:
my.namespace.NonSerializableClass
Run Code Online (Sandbox Code Playgroud)
我认为发生这种情况是因为 JSF 无法恢复nonSerializableClassInstance:
Caused by: java.lang.IllegalStateException: java.lang.InstantiationException: com.foobar.utils.text.Period
at javax.faces.component.StateHolderSaver.restore(StateHolderSaver.java:110)
at javax.faces.component.ComponentStateHelper.restoreState(ComponentStateHelper.java:292)
at javax.faces.component.UIComponentBase.restoreState(UIComponentBase.java:1444)
at javax.faces.component.UIOutput.restoreState(UIOutput.java:255)
at javax.faces.component.UIInput.restoreState(UIInput.java:1359)
Run Code Online (Sandbox Code Playgroud)
一个额外的问题:不让支持 bean 可序列化可以吗?那么这是否应该阻止这些序列化/反序列化?
一些背景:
我们有很多 3rd 方类,需要在 JSF 中提供表单。问题是我们不能直接在 JSF 页面上使用这些类,因为它们没有实现 Serialized 接口,因此如果 JSF 运行时决定序列化/反序列化页面和组件树,它们将会/应该失败。这些类是“封闭的”,我们不允许修改它们。
运行 Mojarra 2.0.2。
在postgres数据库中,我有一个表,其中包含一个int []字段.
在我的模型中,我有一个映射表的持久bean,包括int []字段.在本课程中,我已经实现了所有需要的setter/getter.
现在,我有一个托管bean,它也扮演控制器角色,并链接到模型bean.所以,在我的xhtml中我试图这样做:
<ui:repeat value="#{drawsetController.selected.editableBaseSetList}" var="baseNumber">
<h:inputText value="#{baseNumber}"/>
</ui:repeat>
Run Code Online (Sandbox Code Playgroud)
baseSetList 是int []数组.
问题是,当我提交表单时,只有这个元素不会更新.初始化正在运行,getter被调用,但不是save,所以它必须是绑定的东西.长话短说,我试图用一个自定义类的ArrayList替换int []数组,它可以包装int(就像一个可写的Integer),但它不起作用.
也许这是重复不能正确绑定,我真的不知道.这是我经过多年的PHP :)后在java中的第一个项目.
可能重复:
为什么+使用Java中的字符串?
以下语句在Java中有效.
int a=50;
String tmp="a = ";
String b=tmp+a;
Run Code Online (Sandbox Code Playgroud)
bString类型现在包含a = 50(作为String).
虽然tmp是String类型并且a是类型int,但是进行连接(即使Java不支持运算符重载).
Java不支持运算符重载的原因之一(与其他语言一样.事实上,我对任何语言都没有深度知识).
Java不支持运算符重载.运算符重载有时是C++程序中模糊不清的原因,而Java设计团队认为它会带来更多麻烦而不是利益.
更多关于它.
该声明如何String b=tmp+a;评估?内部必须有一些等效的运算符重载概念.
只有一个问题:我们能从字面上看到它是如何实现的,或者我们应该只相信" 它只是语言的一个特征 "?
我听说Java编译器使用StringBuilder/StringBuffer(使用该append()方法)来实现这一点,但我不确定.
我正在使用SimpleFormControllerSpring MVC 3.0.2使用Hibernate 在JSP中开发一个应用程序.一切都好.我也Validator用来验证服务器端的表单.它也很顺利.
现在,我需要使用Ajax作为示例,当从下拉列表中选择一个<form:select><form:option></form:option></form:select>国家/地区时,应该从状态下拉列表中的数据库中填充与该国家/地区对应的状态.
我已经在地方使用Ajax做过这样的事情但是没有使用Spring MVC.我已经阅读了很多有关SimpleFormControllerGoogle的教程/文章,但没有人使用过Ajax.我找不到关于如何使用Ajax的单一想法SimpleFormController.
使用带注释的控制器(@Controller),事物可以变得简单,因为可以使用@RequestMapping注释来映射方法(尽管我还没有使用它,但我认为我可以).
但是SimpleFormController,我对如何处理Spring控制器中的Ajax请求(要映射哪些方法以及如何处理)没有任何确切的想法.有了SimpleFormController,我通常与关联onSubmit(),showForm()和referenceData()方法.
您能否请一些关于如何制作Ajax请求SimpleFormController,可以映射哪些方法以及如何映射的想法?(我不再需要完整的代码.一个非常简单的例子(当且仅当它是可能的)或者更具体的链接,其中SimpleFormController解释了使用Ajax 对我来说是足够的研究).
我正在尝试使用以下API获取实时汇率.
"http://www.exchangerate-api.com/INR/USD/1?k=FQRxs-xT2tk-NExQj"
Run Code Online (Sandbox Code Playgroud)
当我点击一个按钮时,它会提醒率并且工作得很好.我正在使用以下Ajax代码.
<script type="text/javascript" language="javascript">
function testCurrencyRate()
{
$.ajax({
datatype:"html",
type: "GET",
url: "ajax/LiveCurrencyRate.php",
data: "t="+new Date().getTime(),
success: function(response)
{
alert(response);
},
error: function(e)
{
alert('Error while fetchig the live currency rate.');
}
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
Ajax请求转到LiveCurrencyRate.php如下页面.
$url="http://www.exchangerate-api.com/INR/USD/1?k=FQRxs-xT2tk-NExQj";
$result = file_get_contents($url);
echo $result;
Run Code Online (Sandbox Code Playgroud)
并且<form></form>它包含唯一的按钮,当单击该按钮时,会在此URL上发出Ajax请求ajax/LiveCurrencyRate.php.
<form id="testForm" name="testForm" action="" method="post">
<input type="submit" id="btnTestCurrencyRate" name="btnTestCurrencyRate" value="Test" onclick="testCurrencyRate();"/>
</form>
Run Code Online (Sandbox Code Playgroud)
一切都好.然而,问题出现了,当我从改变按钮类型type="button"来type="submit",这是行不通的.Ajax函数的错误部分中的警告框只显示警报框一段时间,突然它消失了.我找不到任何可能阻止此请求完成的合理原因.在我之前的项目中,同样的事情对我有用,但我用它XMLHttpRequest来制作Ajax请求.这里出了什么问题?
在非事务性环境中,可以在 Hibernate 中批量删除多行,如下所示。
Session session=HibernateUtils.getSessionFactory().getCurrentSession();
session.beginTransaction();
String temp[]=request.getParameter("delIds").split(",");
int len=temp.length;
countryService.delete(temp);
Configuration configuration=new Configuration();
configuration.setProperty("hibernate.jdbc.batch_size", "50");
for(int i=0;i<len;i++)
{
Country c=(Country)session.get(Country.class, new Long(temp[i]));
if(i%50==0)
{
session.flush();
session.clear();
}
session.delete(c);
}
//session.flush();
session.getTransaction().commit();
Run Code Online (Sandbox Code Playgroud)
每批将考虑删除 50 行。
如何在交易环境中实现同样的功能?
@Service
@Transactional(readOnly = true, propagation=Propagation.REQUIRES_NEW)
final public class CountryDAO implements CountryService
{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
@SuppressWarnings("unchecked")
@Transactional(readOnly=false, propagation=Propagation.REQUIRES_NEW)
public void delete(String[] id)
{
int len=id.length;
Session session=sessionFactory.getCurrentSession();
for(int i=0;i<len;i++)
{
Country country=(Country)session.get(Country.class, Long.valueOf(id[i]));
session.delete(country);
} …Run Code Online (Sandbox Code Playgroud) 我使用Spring安全性3.2.0和相同版本的Spring框架.Spring安全性在我的项目中运行良好.为了保护我的DAO类(和其他类)中的方法,我想使用以下切入点方法(在spring-security.xml文件中).
<global-method-security>
<protect-pointcut expression="execution(*controller.*.*(..))" access="ROLE_ADMIN"/>
</global-method-security>
Run Code Online (Sandbox Code Playgroud)
我希望指定的切入点表达式能够保护controller包内所有类中的所有方法,并且只能由具有ROLE_ADMIN指定权限的用户访问.
但是当我尝试使用这个表达式时,进程终止时会出现以下保存spring-security.xml文件的异常.
PropertyAccessException 1:org.springframework.beans.MethodInvocationException:属性'pointcutMap'抛出异常; 嵌套异常是java.lang.IllegalArgumentException:切入点格式不正确:在字符位置26执行时期望'名称模式'(控制器.*(..))^
我正在尝试使用3.4.1 元素部分的保护切入点子部分的添加安全性切入点中的参考文档所指定的方法.<global-method-security>
这种情况下正确的表达式语法是什么?
编辑:
使用protect-pointcut添加安全切入点
使用protect-pointcut特别强大,因为它允许您只使用简单的声明将安全性应用于许多bean.请考虑以下示例:
<global-method-security>
<protect-pointcut expression="execution(* com.mycompany.*Service.*(..))" access="ROLE_USER"/>
</global-method-security>
Run Code Online (Sandbox Code Playgroud)
这将保护应用程序上下文中声明的bean上的所有方法,这些bean的类在com.mycompany包中,其类名以"Service"结尾.只有具有该ROLE_USER角色的用户才能调用这些方法.与URL匹配一样,最具体的匹配必须首先出现在切入点列表中,因为将使用第一个匹配表达式.安全注释优先于切入点.
复制并粘贴参考文档中解释的部分(因为有人可能会发现滚动文档很繁琐).
jsf ×4
java ×3
ajax ×2
hibernate ×2
spring ×2
equals ×1
facelets ×1
faces-config ×1
java-time ×1
javascript ×1
jquery ×1
jsf-2 ×1
jsp ×1
managed-bean ×1
pointcut ×1
spring-mvc ×1
string ×1
transactions ×1
uirepeat ×1