最近开始学习使用springmvc, 第一个列子是配置了一个视图
controller代码如下:
package com.cotroller;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import vo.Shop;
/**
* Created by on 2017/8/30.
*/
@Controller
@RequestMapping(value="/hello")
public class HelloController {
@RequestMapping(value="/world",method= RequestMethod.GET)
public String hello(Model model){
model.addAttribute("msg", "你好spring mvc");
return "index";
}
@ResponseBody
@RequestMapping(value="/string",method= RequestMethod.GET,produces="text/html;charset=UTF-8")
public String string(Model model){
model.addAttribute("msg", "你好spring mvc");
return "你好123333";
}
@ResponseBody
@RequestMapping(value="/json/{name}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
public
Shop getShopInJSON(@PathVariable String name) {
System.out.println("-----请求json数据--------");
Shop shop = new Shop();
JsonProcessingException e=null;
shop.setName(name);
shop.setStaffName(new String[]{"mkyong1", "mkyong2","fjjfjfj"});
return shop;
}
}
hello方法返回了视图的名称“index”,"index"的配置如下:<!-- 视图层配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".html"/> </bean>
然后,tomcat起来,请求链接http://localhost:8080/hello/world一直是404,看了很多网上其他小伙伴的解决办法都不行,后来发现是视图的配置出了问题。。。。。springmvc不支持html的视图, 将配置改成 jsp的就好了,小问题埋大坑,花了大把时间找这个问题。。。。
现在的配置:<!-- 视图层配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean>