java 前台使用枚举方法(一)

枚举值封装:

http://blog.csdn.net/hanjun0612/article/details/72845960


一  基本类型

这里接着说前台调用枚举值。


首先,controller层

读取对应的状态1,放入modelmap

@Controller
@RequestMapping("test")
public class TestController extends TestBaseController {

    @RequestMapping(value = "test1.do")
    public ModelAndView test1(HttpServletRequest request, HttpServletResponse response, ModelMap mode) throws Exception {
        mode.addAttribute("item", 1);
        return new ModelAndView("test/test");
    }


}

前台显示:

<%@ page import="com.sanju.sanjuSCM.utils.EnumHelper" %>
<%@ page import="com.sanju.sanjuSCM.model.EnumList.EnumList" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


<%=EnumHelper.getEnumItem(EnumList.IsDeleteEnum.class, Integer.valueOf((Integer) request.getAttribute("item"))).getItemCN()%>
<%=EnumHelper.getEnumItem(EnumList.IsDeleteEnum.class, Integer.valueOf((Integer) request.getAttribute("item"))).getItemEN()%>



至此,我们也了解了。其实

mode.addAttribute("item", 1);

只是将数据放入了request.getAttribute("item")中。

前台可以通过这个方法获取到,并且通过前一篇  http://blog.csdn.net/hanjun0612/article/details/72845960  的枚举操作方法,来转换对应的中英文



二  泛型集合


Controller:

@Controller
@RequestMapping("test")
public class TestController extends TestBaseController {

    @RequestMapping(value = "test1.do")
    public ModelAndView test1(HttpServletRequest request, HttpServletResponse response, ModelMap mode) throws Exception {
        List list=new ArrayList();
        SysUser u1=new SysUser();
        u1.setAccount("a");
        u1.setUsernamecn("Testa");
        u1.setIslock(1);
        list.add(u1);

        SysUser u2=new SysUser();
        u2.setAccount("b");
        u2.setUsernamecn("Testb");
        u2.setIslock(0);
        list.add(u2);
        mode.addAttribute("list", list);
        return new ModelAndView("test/test");
    }

}

JSP:

1 使用:<%%>解决


    <%
        for(SysUser v:(List)request.getAttribute("list")){
            %>
    
    <%
        }
    %>

账号 姓名 状态
<%=v.getAccount()%> <%=v.getUsernamecn()%> <%=EnumHelper.getEnumItem(EnumList.IsDeleteEnum.class, v.getIslock()).getItemCN()%>


2 使用 : EL表达式

<%int i=0;%>
    
        
            ${item.account}
            ${item.usernamecn}
            <%=EnumHelper.getEnumItem(EnumList.IsDeleteEnum.class, ((List)request.getAttribute("list")).get(i).getIslock()).getItemCN()%>
        
        <%i++;%>
    





你可能感兴趣的:(Java)