如何使用Spring 4 Rest Controller为Jsp视图服务? [英] How to use Spring 4 Rest Controller to serve for Jsp view?

查看:115
本文介绍了如何使用Spring 4 Rest Controller为Jsp视图服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了用于构建休息服务的Spring 4项目。所以我已经创建了超过50个休息服务,现在需要为此创建web-app。所以我想重用相同的控制器,所以我只需要编写视图(JSP)代码。



对于同样的我googled并找到:



你不应该。 @RestController并不意味着返回视图。这意味着返回将直接写入响应主体的内容。



如果你真的需要,你可以声明你的方法有一个返回类型的View或ModelAndView并返回具有标识视图名称的相应对象。


I have created Spring 4 project for building rest services. So I have created more then 50 rest services, now the requirement came to create web-app for the same. So I want to reuse the same controller, so that I just have to write view(JSP) code only.

For the same I googled and found http://www.mkyong.com/spring-mvc/spring-3-mvc-contentnegotiatingviewresolver-example/ but this gives me an error. Morover my project structure is below.

So where I need to write this config in servlet-context.xml? What is the best way to achieve and how?

Mainly I need ouput in Json and html only. So its fine my http request have an extension .json and .jsp.

Below is code of one of my controller. Below is the way I have created Rest service, now I want this same method to server for JSP pages.

    package com.alumini.spring.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alumini.spring.DAO.CourseDAO;
import com.alumini.spring.model.Alumini;
import com.alumini.spring.model.Course;

@RestController
public class CourseController {

    @Autowired
    private CourseDAO courseDao;

    @RequestMapping(value = "/getCourseList")
    public List<Course> getCourseList(@RequestParam("email") String email,@RequestParam("password") String password) {
        List<Course> listCourse = courseDao.list();
        List<Course> finalList= new ArrayList<Course>();
        for(Course course: listCourse) {
            finalList.add(course);          
        }
        return finalList;
    }

}

Please help me, if any of you have done that earlier?

解决方案

Like @Sotirios Delimanolis said here :

You shouldn't. A @RestController is not meant to return views. It's meant to return something that will be written to the response body directly.

If you really have to, you can declare your method to have a return type of View or ModelAndView and return the appropriate object, with an identifying view name.

这篇关于如何使用Spring 4 Rest Controller为Jsp视图服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆