小编Rom*_*n C的帖子

无法对非静态方法进行静态引用

到目前为止,我有以下代码:

import java.util.Scanner;

public class HallLanceMemoryCalculator {
private double currentValue;

public static int displayMenu(){

    Scanner input=new Scanner(System.in);

    int choice=0;

    while(choice<1||choice>5){      
    System.out.println("1.Add");
    System.out.println("2.Subtract");
    System.out.println("3.Multiply");
    System.out.println("4.Divide");
    System.out.println("5.Clear");

    System.out.println("What would you like to do?");
    choice=input.nextInt();
    }
    return choice;
}

public static double getOperand(String prompt){
    Scanner input=new Scanner(System.in);
    System.out.println("What is the second number?");
    double secondNumber=input.nextDouble();
    return secondNumber;
}

public  double getCurrentValue(){
    return currentValue;
}

public void add(double operand2){
    currentValue+=operand2;
}

public void subtract(double operand2){
    currentValue-=operand2;
}

public void multiply(double operand2){
    currentValue*=operand2;
}

public void …
Run Code Online (Sandbox Code Playgroud)

java static-methods

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

执行junit时如何将参数传递给Spring MVC控制器中的post方法?

我正在对我的 Spring MVC 控制器方法进行单元测试。下面是我尝试进行单元测试的方法,因为它在我启动服务器时工作正常。

每当我点击index页面时,它都会在浏览器上显示三个文本框,我在其中输入数据并按下提交按钮,然后调用以addNewServers正确的值进行。

现在我需要对同样的事情进行单元测试:

@RequestMapping(value = "/index", method = RequestMethod.GET)
public Map<String, String> addNewServer() {
    final Map<String, String> model = new LinkedHashMap<String, String>();
    return model;
}

@RequestMapping(value = "/index", method = RequestMethod.POST)
public Map<String, String> addNewServers(@RequestParam String[] servers, @RequestParam String[] address,
    @RequestParam String[] names) {     


}
Run Code Online (Sandbox Code Playgroud)

下面是我的junit类:

private MockMvc mockMvc;

@Before
public void setup() throws Exception {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");

    this.mockMvc = standaloneSetup(new Controller()).setViewResolvers(viewResolver).build();
}

@Test
public void test04_newServers() throws …
Run Code Online (Sandbox Code Playgroud)

java junit spring spring-mvc spring-test-mvc

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

使用ThreadLocal的最佳方法是什么 - 通过静态或非静态方法?

对于我的应用程序,我必须将某些信息携带到应用程序的每一层(一个示例可能是为传入请求生成的唯一事务ID).

所以,我打算创建一个引用的类ThreadLocal<ConcurrentMap<String, Object>>.

感谢有人可以提供帮助; 我有一个混乱,出于以下方法,哪一个更好:

1. AppContext.java有静态方法

public final class AppContext {
  private static final ThreadLocal&lt;ConcurrentMap&lt;String, Object&gt;&gt; context = new ThreadLocal<>();

  public static void set(final String key, final Object value) {
    // set key value in thread local
  }

  public static Object get(final String key) {
    // get value from thread local
  }
}
Run Code Online (Sandbox Code Playgroud)

2.具有非静态方法的AppContext.java

public final class AppContext {
  private static final ThreadLocal&lt;ConcurrentMap&lt;String, Object&gt;&gt; context = new ThreadLocal<>();

  public void set(final String key, final Object value) { …
Run Code Online (Sandbox Code Playgroud)

java thread-local

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

如何在没有反射的情况下确定java中泛型属性的类类型?

我有以下课程:

public final class Param<T>
{
    private final String name;
    private T value;
    public Param(String name)
    {
        this.name = name;
    }   

    public void setValue(T value) { this.value = value;}
    public T getValue() { return value; }
}
Run Code Online (Sandbox Code Playgroud)

我会用以下方式实例化它:

Param<Long> foo = new Param<>("bar");
Param<String> baz = new Param<>("foo");
Run Code Online (Sandbox Code Playgroud)

我想向public boolean validate(String value)此类添加一个方法,该方法将执行以下操作:

public boolean validate(String value)
{
    try
    {
        if (this.value instanceof String)
            return true;

        if (this.value instanceof Long)
            this.value = (T) Long.valueOf(value);
        else if (this.value instanceof Integer) …
Run Code Online (Sandbox Code Playgroud)

java generics

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

无法使用for循环在java中初始化二维数组

我无法使用for循环初始化二维数组

import java.util.Scanner;

   public class Quater{
      public static void main(String[] args){
        //declare an array
        double product[][]=new double[3][2];

        //declare a Scanner object
        Scanner userInput=new Scanner(System.in);

        //ask the user for input

        System.out.println("Please enter your data");
        for(int i=0;i<=1;i++)
        {
          for(int j=0;j<=2;j++){
          System.out.println(" enter your data");

          product[i][j]=userInput.nextDouble();
        } 
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

问题是当我尝试输入第三个数字时,这会关闭而不能正常工作,但是,如果我将它设为新的双[100] [100],这可以工作并允许我输入6个数字.

java arrays for-loop

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

Struts + Hibernate:@SessionTarget无效

我正在使用如下所示在我的DAO类中struts2-fullhibernatecore-plugin-2.2.2-GA.jar注入一个session:

public class UserDAO {
    @SessionTarget
    Session session;

    @TransactionTarget
    Transaction transaction;

    public List<User> getUsers() {
        return session.createQuery("from user").list();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我得到了

java.lang.NullPointerException
  com.wudi.DAO.UserDAO.getUsers(UserDAO.java:28)
  com.wudi.action.UserListAction.execute(UserListAction.java:24)
  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  java.lang.reflect.Method.invoke(Method.java:606)
  ...
Run Code Online (Sandbox Code Playgroud)

根据调试输出,session并且transaction在UserDAOARE null.

一些文件供参考:

User.java:

@Entity
@Table(name = "user")
public class User implements Serializable {

    @Id
    @GeneratedValue
    private int id;

    @Column
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id; …
Run Code Online (Sandbox Code Playgroud)

java mysql dao hibernate struts2

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

在 JAVA 类中使用嵌入枚举有什么好处?

enum在JAVA中使用嵌入类有什么好处?就像这样:

public class Outter {
    public enum Color {
        WHITE, BLACK, RED, YELLOW, BLUE
    }
}
Run Code Online (Sandbox Code Playgroud)

java enums

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

无法获取struts.xml连接到"http://struts.apache.org/dtds/struts-2.0.dtd"

我在Rational Application Developer(RAD)v 7.5.3中使用Struts2.

我在struts.xml文件中不断收到以下警告:

该文件无法验证为XML Schema" http://struts.apache.org/dtds/struts-2.0.dtd ",指定为无法找到该文件的语法.

我在src文件夹中有struts.xml文件.

我正在使用的Struts2 jar文件是struts2-core-2.3.16.3.jar.

我在struts.xml文件中有以下内容:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
Run Code Online (Sandbox Code Playgroud)

我已经尝试将struts-2.0.dtd更改为struts-2.3,以便它看起来像这样:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
Run Code Online (Sandbox Code Playgroud)

(当我第一次发布这个问题时,我忘记在上面的第二个例子中将"DTD Struts配置2.0"更改为"DTD Struts配置2.3".我更正了它但之后仍然遇到了问题.感谢Roman C指出了这一点.)

但由于某种原因它仍然无法连接.

我把这些struts网址放在我的浏览器中,看看我的浏览器是否可以访问它们,他们可以.当您在浏览器中访问URL时,浏览器会提示您下载该文件.

我可以运行我的示例Struts2应用程序,但此验证警告不会消失.我想知道是否有人知道可能导致它的原因以及如何摆脱警告.

谢谢.

更新:

我试过了

    <!DOCTYPE struts-config PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
        "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
Run Code Online (Sandbox Code Playgroud)

这很有效.显然,我得到了验证错误,因为我的工作区使用的是Struts2,而不是Struts1.但URL工作.

我试过了

    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
Run Code Online (Sandbox Code Playgroud)

它没有用.我甚至试过了

    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://jakarta.apache.org/struts/dtds/struts-2.3.dtd">
Run Code Online (Sandbox Code Playgroud)

它仍然无法正常工作.

我正在使用Struts2与2.3.16.3.

出于某种原因,它不喜欢 …

java xml validation dtd struts2

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

Struts &lt;logic:notEmpty&gt;无法正常工作或bean属性未正确写入JSP?

我正在开始为一个我不太熟悉的程序进行错误修复。我已经更改了其中一个JSP的一段代码。我需要它来打印以下两种格式之一的名称(取决于中间名属性是否存在):

姓,名M。

姓,名

我相信,当且仅当该属性存在时,我的代码才应打印中间的首字母和句点,但对于列表中的每个名称,它都将打印:

姓氏,名字。

它不打印中间首字母或中间首字母为空字符串,后跟句点。

相关代码如下:

<html:link styleClass="recordLink" action="/secure/admin/users?actionMethod=details" paramId="userId" paramName="users" paramProperty="userId">
    <bean:write name="users" property="lastName"/>,
    <bean:write name="users" property="firstName"/>
    <logic:notEmpty name="users" property="middleName">
        <bean:write name="users" property="middleName"/>.
    </logic:notEmpty>
</html:link>
Run Code Online (Sandbox Code Playgroud)

为什么<logic:notEmpty>标签不起作用?middleName如果该属性不存在,是否可以将其确定为非空?我的语法有问题吗?

我也尝试过使用JSTL标记,但是无法在OC4J中使用它(错误:“ http://java.sun.com/jsp/jstl/core ”不是注册的TLD名称空间。)

java jsp struts jstl struts-tags

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

jQuery进度栏服务器端

我在服务器端(在db中运行查询数量)的过程运行时间长(不确定时间),耗时超过30秒。我想向用户显示进度百分比。我在应用程序中使用jquery,struts。有办法吗?

ajax jquery struts2 jquery-ui struts2-jquery

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