1. 程式人生 > >java controller控制器怎麼把資料帶回前端

java controller控制器怎麼把資料帶回前端

controller控制器怎麼把資料帶回前端:

1.先是在controller控制器的類裡寫了,先是在方法中定義一個Map<String, Object> map引數,然後給map put相對應的值,return回到前端就可以獲取到相對應到的值:

@Controller
@RequestMapping("/www")
public class HomeController extends BaseController {
	@Autowired
	private HomeService homeService;
	//首頁框架
	@RequestMapping("/index")//先是在方法中定義一個Map<String, Object> map引數
	public String index(HttpServletRequest request,
			HttpServletResponse response, Map<String, Object> map) throws IOException {
		UserVo user = ((AcegiHelper) SpringBeanManager.getBean("acegiHelper")).getSessionUser();
		String defaultPortalOrgID=user.getDefaultPortalOrgID();
		SysPortal sysPortal=homeService.getSysPortalByOrgId(defaultPortalOrgID);
		String logurl="/www/images/LOGO.png";
		String mhName="企業資訊門戶";
		String mainUrl= "/www/main";
		if(sysPortal!=null){
			if(!StringTools.isEmpty(sysPortal.getLogo())){
				logurl=sysPortal.getLogo();
			}
			if(!StringTools.isEmpty(sysPortal.getName())){
				mhName=sysPortal.getName();
			}
			if(!StringTools.isEmpty(sysPortal.getUri())){
				mainUrl=sysPortal.getUri();
			}
		}
		if(!mainUrl.startsWith("http")){
			mainUrl=request.getContextPath()+mainUrl;
		}
		map.put("logurl", logurl);
		map.put("mhName", mhName);
		map.put("mhUrl", mainUrl);
		return "/www/indexPage";
	}

2.前端使用到的:

<span class="logotext">${mhName}</span>

解釋:${mhName}中的mhName變數就是在controller類方法中map.put("mhName", mhName);put進去的變數。<span class="logotext">${mhName}</span>中的${mhName}編譯後就會把值替換掉了。