从另一个控制器访问controlleradvice对象

时间:2018-02-10 11:05:43

标签: java spring spring-mvc

我有一个# Determination of laminar burning velocities from experimental data using constant volume pressure rise method. import numpy as np import openpyxl as xl import matplotlib.pylab as plt from lmfit import Model plt.rc('font',family='Book Antiqua',size=12) p_i=float(2.0) # Initial pressure c=float(2.0265) # Constant V_b=float(1.94e-3) # Volume of combustion bomb k_u=float(1.3) # Specific heat ratio of unburnt mass k_b=float(1.27) # Specific heat ratio of burnt mass # Extracting experimental data from excel. wb = xl.load_workbook("G:/Laminar Paper/Data/T0444CH1.xlsx") ws = wb.active # Calculation of initial voltage from experimental values. num_cells=list(ws['B2':'B1500']) V_initial=[] for row in num_cells: for cell in row: V_initial.append(cell.value) V_av=np.absolute(sum(V_initial)/len(V_initial)) # Average voltage # Determining the final voltage from the experimental values. num_cells=list(ws['B3':'B13252']) V_final=[] # Experimental testing voltages for row in num_cells: for cell in row: V_final.append(cell.value) p=(V_final/np.floor(c))+(p_i-(V_av/np.floor(c))) # Combustion pressure # The time elapse. num_cells=list(ws['A3':'A13252']) time=[] for row in num_cells: for cell in row: time.append(cell.value) # Curve fitting. Func_1=np.polyfit(time, p, 15) Func_2=np.poly1d(Func_1) time_new=np.linspace(time[0], time[-1], 50) # New time values p_new=Func_2(time_new) # New pressure values/filtered pressure dp_dt=np.gradient(p_new,time_new) # Derivative of pressure wrt time # Linear approximation method. x_linear=(p_new-p_i)/(max(p_new)-p_i) # Burnt mass fraction R_b=(pow(((V_b*3)/(4*np.pi)), 1.0/3)) S_u1=((1-(1-x_linear)*(p_i/p_new)**1/k_u)**-2/3) S_u2=S_u1*((p_i/p_new)*1/k_u) S_u3=S_u2*(1/(max(p_new)-p_i))*dp_dt S_u=(R_b/3)*S_u3 # Linear approximation laminar flame speed # Two-zone approach. func_p=(k_b-1)/(k_u-1)+(((k_u-k_b)/(k_u-1))*(p_new/p_i)**((k_u-1)/k_u)) x_2zone=(p_new-p_i*func_p)/(max(p)-p_i*func_p) # Burnt mass fraction S_uz1=((1-(1-x_2zone)*(p_i/p_new)**1/k_u)**-2/3) S_uz2=S_uz1*((p_i/p_new)*1/k_u) S_uz3=S_uz2*(1/(max(p_new)-p_i))*dp_dt S_uz=(R_b/3)*S_uz3 # Two-zone laminar flame speed # Model function. def mod_m(n,su0=1,alpha=1): return su0*(n)**alpha # S_u and pressure ratio array data for fitting and calculating S_u0 and T_0. n=np.array([1.78759326,1.91173221,2.05673713,2.22911249,2.4360555,2.68433232,2.97885016]) m=np.array([0.20791161,0.20332239,0.20206572,0.20260075,0.20292399,0.20115307,0.19601662]) # Fitting model. model=Model(mod_m) # Making a set of parameters (for 'su0' and 'alpha'): params=model.make_params(s_u0=10) # Setting min/max bounds on parameters: params['alpha'].min=0.0 params['su0'].min=0.0 params['su0'].max=1e2 # Run the fit with Model.fit(Data_Array, Parameters, independent vars). result=model.fit(m,params,n=n) # Plotting. plt.plot(n,result.best_fit,'b*-', label='Fit') plt.plot(p_new/p_i,S_u,'k--',linewidth='2',label='Linear') plt.plot(p_new/p_i,S_uz,'r-',label='Two-zone') plt.xlabel('Pressure ratio $p/p_i$') plt.ylabel('Laminar flame speed $(S_u)$ [m/s]') plt.legend(loc='lower right') plt.savefig('Laminar.png') plt.show() # Printing report with results and fitting statistics. print(result.fit_report()) 课程,用于在整个应用程序中设置用户个人资料信息。通过这种方式,我获得了每个JSP中的用户配置文件。但是,我尝试在@ControllerAdvice中尝试访问该对象,但没有成功:

@Controller

但我只得到一个@ControllerAdvice public class CommonControllerAdvice { @ModelAttribute("PROFILE") public Profile populateUserProfile(HttpSession session){ return (Profile) session.getAttribute("PROFILE"); } } @Controller public class ActivityController { @GetMapping("/view/activity/{id}") public ModelAndView getActivity(ModelAndView modelAndView, @PathVariable Integer id) { Profile profile = (Profile) modelAndView.getModel().get("PROFILE"); ... ... } } 因为配置文件为空。但是,我知道它不是NullPointerException,因为我可以在相关的JSP中使用它。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。只需将@ModelAttribute类中定义的@ControllerAdvice作为参数传递,而不是尝试从ModelAndView获取它。

@Controller
public class ActivityController{

   @GetMapping("/view/activity/{id}")
   public ModelAndView getActivity (
       ModelAndView modelAndView,
       @ModelAttribute Profile profile,
       @PathVariable Integer id) {
            //Profile object is well populated
            //However I don't understand why this model is empty
          ModelMap model = modelAndView.getModelMap();
          ...
   }
}

它解决了我的问题,我对解决方案很满意,但是,我希望能够直接在ModelMap中访问此信息,并且它是空的。我想知道原因。