小编ash*_*ram的帖子

如何避免java中的数字格式异常?

在我的日常Web应用程序开发中,有许多情况需要从用户那里获取一些数字输入.

然后传递此数字输入可以是应用程序的服务或DAO层.

在某个阶段,因为它是一个数字(整数或浮点数),我们需要将它转换为Integer,如下面的代码片段所示.

String cost = request.getParameter("cost");

if (cost !=null && !"".equals(cost) ){
    Integer intCost = Integer.parseInt(cost);
    List<Book> books = bookService . findBooksCheaperThan(intCost);  
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我必须检查输入是否为空或是否没有输入(空白)或有时可能存在非数字输入,例如,等等,测试等.

处理此类情况的最佳方法是什么?

java numberformatexception

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

有没有办法在Spring MVC Test中使用AssertJ断言?

我已经在我的项目中使用AssertJ一段时间了.最近我开始使用Spring MVC Test来测试Spring MVC控制器.

但是我没有得到如何使用AssertJ.我在网上看到的所有例子都使用Hamcrest和Spring MVC Test.

下面是使用Hamcrest API的示例.

mockMvc
                .perform(get("/user?operation=userList"))
                .andExpect(status().isOk())
                .andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, userList))
                .andExpect(view().name(UserController.VIEW_USER_LIST))
                .andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, hasSize(2)))
                .andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, hasItem(
                        allOf(
                                hasProperty("id", is(1L)),
                                hasProperty("description", is("Lorem ipsum")),
                                hasProperty("title", is("Foo"))
                        )
                )))
                .andExpect(model().attribute(UserController.MODEL_ATTRIBUTE_USER_LIST, hasItem(
                        allOf(
                                hasProperty("id", is(2L)),
                                hasProperty("description", is("Lorem ipsum")),
                                hasProperty("title", is("Bar"))
                        )
                )));
Run Code Online (Sandbox Code Playgroud)

spring-mvc hamcrest spring-test assertj

14
推荐指数
1
解决办法
1858
查看次数

如何将自定义ApplicationContextInitializer添加到Spring启动应用程序?

向Spring Web应用程序添加自定义ApplicationContextInitializer的一种方法是将其添加到web.xml文件中,如下所示.

<context-param>
    <param-name>contextInitializerClasses</param-name>
    <param-value>somepackage.CustomApplicationContextInitializer</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

但是因为我使用的是spring boot,有没有什么办法可以创建web.xml来添加CustomApplicationContextInitializer?

spring spring-boot

14
推荐指数
2
解决办法
2万
查看次数

如何使用Display标记Java库对列标题进行分组?

我在我的Web应用程序中使用Display标记 Java库.我需要对一些列标题("常规"和"Office信息")进行分组,如以下示例所示.

在此输入图像描述

java displaytag

13
推荐指数
1
解决办法
5027
查看次数

如何在oracle中将月份数转换为完整月份名称?

我的桌子上有一个月的专栏.月份数据存储在此月份列中,例如1表示1月份,2表示为feb,依此类推.

如何将数字转换为月份名称,如1月,2月,3月等.

sql oracle

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

@Autowire不在Spring安全自定义身份验证提供程序中工作

我们有Spring MVC应用程序.我们正在尝试将Spring安全性集成到其中.

我们编写了自定义身份验证提供程序,它将执行身份验证工作.

以下是我的自定义身份验证提供程序的代码.

    public class CustomAuthenticationProvider extends DaoAuthenticationProvider {

    @Autowired
    private AuthenticationService authenticationService;

    @Override
    public Authentication authenticate(Authentication authentication) {

        CustomAuthenticationToken auth = (CustomAuthenticationToken) authentication;

        String username = String.valueOf(auth.getPrincipal());
        String password = String.valueOf(auth.getCredentials());

        try {

            Users user = new User();
            user.setUsername(username);
            user.setPassword(PasswordUtil.encrypt(password));

            user = authenticationService.validateLogin(user);

            return auth;
        } catch (Exception e) {
            throw new BadCredentialsException("Username/Password does not match for " + username);
        }
    }

    @Override
    public boolean supports(Class<? extends Object> authentication) {
        return (CustomAuthenticationToken.class.isAssignableFrom(authentication));

    }
}
Run Code Online (Sandbox Code Playgroud)

这里我在以下行获取NullpointerException

user = authenticationService.validateLogin(user); …
Run Code Online (Sandbox Code Playgroud)

spring-mvc spring-security autowired

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

在Spring启动应用程序中重定向到自定义错误页面时出错

我正在使用spring boot并且我创建了以下自定义错误控制器来处理错误:

@Controller
public class AppErrorController implements ErrorController {

    private static final String PATH = "/error";

    @RequestMapping(value = "/pageNotFound", method = { RequestMethod.GET, RequestMethod.POST })
    public String pageNotFound() {
        return "pageNotFound";
    }

    @RequestMapping(value = "/accessDenied", method = { RequestMethod.GET, RequestMethod.POST })
    public String accessDenied() {
        return "accessDenied";
    }

    @RequestMapping(value = PATH)
    public String error() {
        return "error";
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在应用程序中收到错误时,它应该重定向到自定义错误页面.相反,我在日志中收到以下错误,甚至错误页面都没有显示:

2016-03-09 09:43:21.224 DEBUG 3126 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot

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

使用jsPDF创建的pdf中的重复内容

我正在使用jsPDF将html转换为pdf.在某些情况下,html具有svg图表,一些数据在生成的pdf中重复.

例如,如果图表有图例,它们就会重复.请参见下面的截图.城市名称和百分比重复出现.

在此输入图像描述

以下是创建pdf的代码.

pdf.addHTML($("#page1"), options, function(){
            pdf.addPage();
            pdf.addHTML($("#page2"), options, function(){
                pdf.addPage();
                pdf.output('dataurlnewwindow');
            });
        });
Run Code Online (Sandbox Code Playgroud)

编辑1:

这是我到目前为止所想到的.

<div id="outerDiv">
    <div id="pieChart"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

当我这样做时,pdf.addHTML($("#pieChart")这里没有问题.

但是,当我这样做pdf.addHTML($("#outerDiv"),然后标签重复.

这就是我生成c3js图表的方法

var pieChart  = c3.generate({
            bindto: '#pieChart',
Run Code Online (Sandbox Code Playgroud)

编辑2: -

以下是我的整个代码.

<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.css">
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.js"></script>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
    <script type="text/javascript" src="http://gabelerner.github.io/canvg/rgbcolor.js"></script>
    <script type="text/javascript" src="http://gabelerner.github.io/canvg/StackBlur.js"></script>
    <script type="text/javascript" src="http://gabelerner.github.io/canvg/canvg.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.5.0-alpha1/html2canvas.js"></script>

    <script type='text/javascript'>
    function replaceAllSVGsWithTempCanvas(elemSelector) {
        var svgElements = $(elemSelector).find('svg');

        //replace all …
Run Code Online (Sandbox Code Playgroud)

javascript jspdf

10
推荐指数
1
解决办法
1655
查看次数

是否有任何设计模式可根据设备类型在数据之间切换?

我们有一个基于Spring MVC的Web应用程序.现在我们需要修改此应用程序,以便在智能手机上正确呈现.

为此,我们将为智能手机创建单独的JSP.因此,请求来自浏览器,我们将检查请求是否来自桌面然后我们将显示正常的JSP,或者如果请求来自移动,那么我们将显示智能手机的JSP.

我们将使用spring Mobile.

在某些情况下,我们还希望限制智能手机上的数据.我们可能不会在JSP中显示所有数据.

例如,我们可能只需在菜单中显示几个项目.桌面Web应用程序将显示完整菜单,而智能手机将显示较少的菜单项.即使我们将为桌面菜单和移动菜单使用不同的JSP,菜单项也来自数据库.

是否有任何设计模式可以帮助我们?我们不想写那些if else条件来检查设备类型.

java design-patterns spring-mvc spring-mobile

9
推荐指数
1
解决办法
1476
查看次数

如果PC连接到网络打印机,如何检查java?

基本上,我需要检查n/w打印机的状态,如果它打开或不打开.在java中有没有办法做到这一点?

是否有任何第三方API或工具?

我尝试在java中使用PrintServiceLookup,但它不提供状态,如果它打开或不打开.

此外,如果在java中不可能,是否有任何命令可以在Windows中运行,将提供打印机的状态?

然后我可以在java中运行这个命令并检查.

java network-printers

9
推荐指数
1
解决办法
1846
查看次数