Spring Boot rest controller返回jsp

时间:2016-03-22 13:19:47

标签: java rest jsp spring-mvc spring-boot

我有一个带有休息控制器的springboot应用程序。控制器通过URL接收来自用户的输入,执行一些操作,并在屏幕上打印出结果。我想返回一个jsp页面,而不是格式化那些数据很好,整洁,供用户阅读。

以前当我只是吐出屏幕上的值时,这是我的休息控制器:

@RequestMapping(value = "/weather", method = RequestMethod.GET)
public String getWeather(@RequestParam(value="zip") String zip) {
    StringBuilder sBuffer = new StringBuilder();

    Weather weather = weatherOperation.getWeather(zip);
    sBuffer.append(weather.getFarenheit());
    sBuffer.append("<br>");
    sBuffer.append(weather.getChanceOfPercipitation());
    sBuffer.append("<br><br><br>");

    return sBuffer.toString();
}

我通过访问网址测试了一百多次:

http://localhost:8080/weather?zip=10001

在我的所有测试用例中,它都像魅力一样。

这是我的休息控制器现在试图返回一个jsp页面:

@RestController
public class AlertController {
    private final WeatherOperation weatherOperation;

    public AlertController(WeatherOperation weatherOperation) {
        this.weatherOperation = weatherOperation;
    }

    // RESTful method
    @RequestMapping(value="/weather", produces={"application/json"})
    @ResponseStatus(HttpStatus.OK)
    public @ResponseBody Weather listWithMarshalling(String zip) {
        return weatherOperation.getWeather(zip);
    }

    // View-based method
    @RequestMapping("/weather")
    public String list(@RequestParam(value="zip") String zip, Model model) {
        model.addAttribute(listWithMarshalling(zip));
        return "views/testpage";
    }
}

这是我的javaconfig:

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {
    @Autowired
    ServletContext servletContext;

    // Will map to the JSP page: "WEB-INF/jsp/views/testpage.jsp"
    @Bean(name="jspViewResolver")
    public ViewResolver getJspViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("WEB-INF/jsp/views");
        resolver.setSuffix(".jsp");
        resolver.setOrder(1);
        return resolver;
    }
}

最后是我的文件结构:

Project
 +-- webapp
 |  |  
 |  +-- WEB-INF
 |  |
 |  |   +-- jsp
 |  |   |
 |  |   |   +-- views
 |  |   |   |
 |  |   |   |   +-- testpage.jsp

但每次我尝试访问网址http://localhost:8080/weather?zip=10001时,我只会获取字符串“views / testpage”而不是实际页面。知道我做错了吗?

0 个答案:

没有答案