您的位置:首页 > 编程语言 > Java开发

Java文件编译的两种方式以及在SpringMVC传参中带来的问题

2013-08-26 10:31 513 查看
一、基本概念

java编译成.class 有两种方式。使用javac,默认使用的release方式,而使用的MyEclipse工具,用的是debug模式。值得注意的是使用release模式下对于函数参数会改变。

public class Test{

private static void sayHello(){

System.out.println("Hello world");

}

public static void main(String[] args){

sayHello();

}

}

以上代码分别用javac命令和MyEclipse IDE编译,然后使用jd-gui.exe查看Test.class,可以发现区别。

使用MyEclipse编译(debug)

public class Test

{

private static void sayHello()

{

System.out.println("Hello world");

}

public static void main(String[] args) {

sayHello();

}

}

使用javac编译(release)

public class Test

{

private static void sayHello()

{

System.out.println("Hello world");

}

public static void main(String[] paramArrayOfString) {

sayHello();

}

}



二、发现问题

以SpringMVC为例

@RequestMapping(/test/{str})

public String test(@PathVariable String str){

System.out.println(str);

return null;

}

实际项目部署使用的是release版本,这样str经过编译后就和RequestMapping中的{str}这样就对应不起来了。而这样的问题在开发中是不会发现的。这也可以解释为什么在spring MVC 中controller的注解初始化参数建议指定名称。建议写法如下:

@RequestMapping(/test/{str})

public String test(@PathVariable("str") String str){

System.out.println(str);

return null;

}



原帖地址:http://blog.csdn.net/ping_qc/article/details/7265814
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐