场景如下:
一般像我们在java中判断一个水果的类型都会写如下代码
class Fruit {
public final static int APPLE = 1;
}
if (a.getType = Fruit.APPLE ) {
System.out.pring("苹果");
}
而不会写如下代码
if (a.getType = 1 ) {
System.out.pring("苹果");
}
现在有如下需求:
需要在jsp中判断水果类型,由于jsp不能获取对象。
<%
使用这个new出来的对象好像只能获取变量不能获取静态变量
%>
求支招。
想要在jsp中写类似如下的代码
if (a.getType = Fruit.APPLE ) {
System.out.pring("苹果");
}
我记得如果你想通过el表达式来获取的话,似乎只能个静态成员加个get方法,不然你就只能用<%%>代码块来访问
纠正一下 判断相等用== 而不是= 一个等于号是赋值操作
例子如下:
代码
package com.demo;
public class Fruit {
public static final int APPLE = 1;
}
jsp页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.demo.Fruit" %>
<%
int type = 1;
if (type == Fruit.APPLE ) {
System.out.pring("苹果");
}
%>
其中 <%@ page import="com.demo.Fruit" %> 用于引入java的类
public class Fruit {
public static final int APPLE = 1;
}
<%
int type = 1;
if (type == Fruit.APPLE ) {
System.out.pring("苹果");
}
%>
jsp中可以直接使用的,就跟在java程序种使用一样,<%%>中的代码就是java代码,并不是el,el是在<%%>之外用{}括起来的
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.demo.Fruit" %>
<%
int type = 1;
if (type == Fruit.APPLE ) {
System.out.pring("苹果");
}
%>
jsp中可以直接使用的,就跟在java程序种使用一样,<%%>中的代码就是java代码,并不是el,el是在<%%>之外用{}括起来的
使用这个静态变量的地方 是在js中 不是 <%%> 中
<%@page import="com.bsteel.shdc.constant.LSConstants"%>