1

我有一个控制器,它返回一个覆盖AbstractPdfView. 效果很好,除了当用户从浏览器保存 PDF 表单时,它会尝试保存请求路径(似乎合理但不是我想要的)。

控制器代码为:

public class MealPlanPDFController {

    private MealPlannerDao mealPlannerDao;
    private UserDao userDao;
    //protected SessionFactory sessionFactory;

    @Autowired
    public void setMealPlanDao(MealPlannerDao mealPlannerDao) {
        this.mealPlannerDao = mealPlannerDao;
    }

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    @Transactional
    protected ModelAndView handleRequest(@PathVariable Long id, HttpServletRequest request) throws Exception {
        System.out.println(request.getContextPath());
        ModelAndView modelView = new ModelAndView(new MealPlanPDFView());
        MealPlan mealPlan = mealPlannerDao.getMealPlan(id, true);
        ServletContext servletContext = request.getSession().getServletContext();
        Resource res = new ServletContextResource(servletContext,"/images/logo.png");

        URL url = res.getURL();
        modelView.addObject("mealPlan",mealPlan);
        modelView.addObject("imageURL",url);
        System.out.println(url.getPath());
        return modelView;
    }
}

所以请求路径是:

api/mealplanpdf/1 其中 1 是 MealPlan 的 ID。

在回应我希望它是mealplan_1.pdf

说得通?知道我会怎么做吗?

谢谢

4

0 回答 0