SSM controller要能跳转页面又要能返回字符串

 SpringMVC因为添加了下面这个bean  视图解析器  当你方法返回的是 json 、字符串等其它值时, 会404

   跳转  jsp/*.jsp页面   

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    </bean>
下面这个返回的是   跳转jsp页面
@RequestMapping(value = "get_xx.do",method = RequestMethod.GET)
    public String get_information(HttpSession session, ModelMap map){
        String currentUser = (String) session.getAttribute("username");
        if(currentUser == null){
           
            return "index";
        }

        map.put("userList",userList);
        return "user_management_show";  //跳转页面
    }

页面有需求是返回字符串

@RequestMapping(value = "findDeptName.do",method = RequestMethod.GET)
    @ResponseBody    //要求返回的是信息   如果去掉就返回  jsp/*.jsp页面
    public String getDeptNameByid(Integer devId){
        return derviceService.getDeptName(devId);  //有注解,所以返回的是查询出来的字符串
    }

@ResponseBody  //有这个注解的时候返回结果直接写入HTTP response body中,不会被解析为跳转路径。比如异步请求,希望响应的结果是json数据,那么加上@responsebody后,就会直接返回json数据。

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huang_ftpjh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值