小编The*_*Man的帖子

连续的String.replace的替代方案

我想替换String输入中的一些字符串:

string=string.replace("<h1>","<big><big><big><b>");
string=string.replace("</h1>","</b></big></big></big>");
string=string.replace("<h2>","<big><big>");
string=string.replace("</h2>","</big></big>");
string=string.replace("<h3>","<big>");
string=string.replace("</h3>","</big>");
string=string.replace("<h4>","<b>");
string=string.replace("</h4>","</b>");
string=string.replace("<h5>","<small><b>");
string=string.replace("</h5>","</b><small>");
string=string.replace("<h6>","<small>");
string=string.replace("</h6>","</small>");
Run Code Online (Sandbox Code Playgroud)

正如你所看到的那样,这种方法并不是最好的,因为每次我都要搜索要替换的部分等,而且字符串是不可变的......输入也很大,这意味着需要考虑一些性能问题.

有没有更好的方法来降低此代码的复杂性?

java string replace

29
推荐指数
4
解决办法
4507
查看次数

使用 HTML 5 更改移动设备上的键盘语言

我有双语 HTML 5 表单,我正在使用 type 属性来帮助更改移动设备的键盘布局。

<input type="tel" />
<input type="email" />
<input type="?" />
Run Code Online (Sandbox Code Playgroud)

这工作正常,因为 tel 值显示数字键盘,而电子邮件显示用于电子邮件输入的自定义键盘。

但我想知道是否有办法指定输入的语言,以便设备切换到适当的键盘?

我随机试过这个:

<input type="text" lang="ar" />
Run Code Online (Sandbox Code Playgroud)

但是什么也没发生,这在 HTML5 或 Javascript 中甚至可能吗?

html javascript jquery angular

8
推荐指数
1
解决办法
2556
查看次数

如何从 Java 中的字符串模板生成字符串?

我希望我的 Java 应用程序从用户读取一个字符串,该字符串可能包含一些标签,例如:

String text = " value 1 = #value1 and value 2 = #value2 ";
int[] intArray = new int[] {4,5};
Run Code Online (Sandbox Code Playgroud)

此外,用户将向应用程序输入一组值。作为程序员,我不知道字符串中值的确切数量。我想以编程方式生成这个 String :

String result = " value 1 = 4 and value 2 = 5 "
Run Code Online (Sandbox Code Playgroud)

为此,我实现了一种搜索#value*正则表达式并将其替换为值堆栈中的第一个元素的方法。它循环直到程序在主字符串中找不到任何#value,问题是对于大文本,程序需要太多时间来执行,考虑到所采用的方法,这是正常的。

我也听说过一些使用 Velocity 和 FreeMarker 的模板技术,但我从未使用过它们(非常欢迎在这一点上进行任何澄清)。

所以我的问题是:解决这个问题的最佳方法是什么(最短的执行时间)?

PS:我不需要代码,我只想要一种可以解决此问题的方法或 API。

java regex string template-engine substring

6
推荐指数
1
解决办法
2751
查看次数

Java.lang.NoClassDefFoundError : org/apache/poi/ss/usermodel/Font

我正在使用Apache POI读取一些 Excel 文件。我创建了一个控制台应用程序来完成这项工作,之后我尝试将代码集成到Spring MVC webApp 中(没什么特别的,我知道)。我在Classpath 中包含了相同的 Jar 文件,但是每当我尝试运行代码时,我都会收到此错误:

java.lang.NoClassDefFoundError : org/apache/poi/ss/usermodel/Font  
Run Code Online (Sandbox Code Playgroud)

我相信 Font 类是在编译时找到的,但不是在运行时找到的。我很确定问题不在于ClassPath,但我不知道如何解决这个问题。

编辑:当我使用 main 方法(在同一个 web 项目中)在一个新类中复制相同的代码,并将我的 web 应用程序作为控制台 java 应用程序运行时,它可以工作。

java excel runtime-error spring-mvc apache-poi

5
推荐指数
1
解决办法
1万
查看次数

在ws端点部署中检测到Apache CXF库

我正在尝试在Wildfly 10.0.0中部署我的WAR应用程序,我遇到了一个常见问题:

"{\"WFLYCTL0080: Failed services\" => 
{\"jboss.deployment.unit.\\\"my-app-ws.war\\\".PARSE\" =>
\"org.jboss.msc.service.StartException in service jboss.deployment.unit.\\\"my-app.war\\\"
.PARSE: WFLYSRV0153: Failed to process phase PARSE of deployment \\\"my-app.war\\\"
  Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: 
  WFLYWS0059: Apache CXF library (cxf-core-3.1.4.jar) detected in ws endpoint deployment; 
  either provide a proper deployment replacing embedded libraries with container 
  module dependencies or disable the webservices subsystem for the current deployment adding 
  a proper jboss-deployment-structure.xml descriptor to it. The former approach is 
  recommended, as the latter approach causes most of the webservices Java EE and …
Run Code Online (Sandbox Code Playgroud)

java soap web-services cxf java-ee

5
推荐指数
1
解决办法
2833
查看次数

意外的交叉连接使用带有hibernate的EntityManager生成的SQL

我正在尝试获取特定组中的用户数量,我已完成以下操作:

CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery<Long> q = cb.createQuery(Long.class);
      q.select(cb.count(q.from(User.class))).where(cb.equal(q.from(User.class).get("userGroup"), groupId));
Run Code Online (Sandbox Code Playgroud)

但是,不是获取组中的用户数,而是将此数字乘以表中所有用户的数量,生成的ORM请求如下所示:

Hibernate: select count(*) as col_0_0_ from app_user user0_ cross join app_user user1_ where user1_.user_group=1
Run Code Online (Sandbox Code Playgroud)

编辑:

这是我的用户模型:

public class User {
    private String userFirstName;
    private String userLastName; 
    /* some stuff */
    @ManyToOne
    private Group userGroup;
    }
Run Code Online (Sandbox Code Playgroud)

我的组模型有一个用@Id和named id注释的int属性.在这种情况下,如何获取组ID的用户数?

sql hibernate jpa entitymanager

3
推荐指数
1
解决办法
714
查看次数

使用keytool更改密码时,从Java Keystore中提取PKCS12文件

我有一个Java密钥库:myKeystore.jks,并给出一个别名:someAlias,我试图p12在更改密码时以格式提取相应的资源.

JKS密码12345678与someAlias密钥的密码相同.

我想用新密码保护我的p12文件: 1122334455

我尝试过的:

keytool -importkeystore -srckeystore myKeystore.jks -destkeystore test.p12 -deststoretype PKCS12 -srcalias someAlias -srcstorepass 12345678 -deststorepass 1122334455

在这种情况下test.p12导出,但我无法读取它,因为密码不正确或文件已损坏.

在此输入图像描述

但是当我尝试这个(保持相同的密码):

keytool -importkeystore -srckeystore myKeystore.jks -destkeystore test.p12 -deststoretype PKCS12 -srcalias someAlias -srcstorepass 12345678 -deststorepass 12345678

我可以test.p12用给定的密码打开我的文件.

PS:我也尝试添加-srckeypass参数,但没有运气.

PS 2:我正在使用keystore explorer 5.11打开我的商店

我错过了什么?

java security jks keytool pkcs#12

3
推荐指数
1
解决办法
3924
查看次数

为什么自动装配不起作用?

我正在使用Hibernate和Spring Mvc开发Spring Web应用程序,我想知道为什么Autowiring只能在控制器内部工作,这是一个简单的例子:

@Controller
@RequestMapping(value="SW/excel")
public class ExcelController
{
   @Autowired
   private BlablaService blablaService;
   @RequestMapping({""})
   public ModelAndView indexPage()
   {
     List<Blabla> blablas=BlablaService.getAllBlablas();
   }
}
Run Code Online (Sandbox Code Playgroud)

这段代码对我来说很好,它返回我在我的数据库中的Blablas列表.但是当我在控制器之外使用我的BlablaService时,它不起作用,这里就是例子

 @Controller
@RequestMapping(value="SW/excel")
public class ExcelController
{
   @RequestMapping({""})
   public ModelAndView indexPage()
   {
     BlablaLister lister= new ExcelExporter();
     List<Blabla> blablas=lister.getList();
   }

}
Run Code Online (Sandbox Code Playgroud)

这是Excel导出器:

Class BlablaLister {
@Autowired BlablaService blablaService;
public List<Blabla> getList()
   {
      return blablaService.getAllBlablas;
   }
}
Run Code Online (Sandbox Code Playgroud)

但我总是得到NullPointerException,只要在控制器中的一个类中使用,getAllBlablas就会返回Null.

java spring hibernate spring-mvc

-2
推荐指数
1
解决办法
131
查看次数