开发者社区> 问答> 正文

spring mvc:resources

mvc:resources 的设置

<mvc:resources mapping="/img/**" location="/img/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/html/**" location="/html/" />


js文件夹在webapp目录下 
在Controller类中

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
     
    static Logger logger=LogManager.getLogger(UserController.class.getName());
     
    @RequestMapping("/login")
    public String login(String userName,String password){
        logger.info("username,password");

在类上和方法上都用@RequestMapping jquery不能用

如果把类上的@RequestMapping("/user")删掉,action路径改下 jquery就可以用了

现在我想保留类上的@RequestMapping("/user") 怎么才能用jquery呢?
用mvc:resources 能解决吗?

展开
收起
a123456678 2016-03-18 15:03:25 2420 0
1 条回答
写回答
取消 提交回答
  • package com.hava.demo.springmvc.onlyone.controller.web;
     
    import com.google.common.collect.Maps;
    import com.hava.demo.springmvc.onlyone.entity.OnlyOne;
    import com.hava.demo.springmvc.onlyone.service.OnlyOneService;
    import com.hava.web.serlvet.QueryRequestUtil;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Page;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.servlet.mvc.support.RedirectAttributes;
     
    import javax.servlet.ServletRequest;
    import javax.validation.Valid;
    import java.util.Map;
     
    /**
     * Created by zhanpeng on 15-2-26.
     * 一般意义上的web只使用GET和POST的HTTP函数,而新的HTTP函数还具有其他,必要在其他的地方进行使用
     * OnlyOne管理的Controller, 使用Restful风格的Urls:
     *
     * List page : GET /onlyone/
     * Create page : GET /onlyone/create
     * Create action : POST /onlyone/create
     * Update page : GET /onlyone/update/{id}
     * Update action : POST /onlyone/update
     * Delete action : GET /onlyone/delete/{id}
     */
     
    @Controller
    //声明把OnlyOneController映射到/demo/onlyone
    @RequestMapping(value = "/demo/onlyone")
    public class OnlyOneWebController {
         
        //TODO:没有添加log
         
        @Autowired
        private OnlyOneService onlyOneService;
         
        //在所有函数运行之前都会执行的内容
        @ModelAttribute
        public void arunBefore()
        {
            System.out.println("This method will run other before.");
        }
         
        //只有update方法有对id属性的需求,因此仅仅在update执行时进行实际执行。
        @ModelAttribute
        public void getOnlyOne(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model)
        {
            System.out.println("This method check the entity id attributes.");
            if(id != -1)
            {
                model.addAttribute("onlyone",onlyOneService.getOnlyOne(id));
            }
        }
         
    //    //一般页面请求
    //    @RequestMapping(method = RequestMethod.GET)
    //    public String list()
    //    {
    //
    //        return null;
    //    }
        //执行函数,基础的增删该查
        //创建或新增
        @RequestMapping(value = "create",method = RequestMethod.GET)
        public String createView(Model model)
        {
            model.addAttribute("onlyone",new OnlyOne());
            model.addAttribute("action","create");
             
            return "demo/onlyone/create";
        }
         
        @RequestMapping(value = "create", method = RequestMethod.POST)
        public String create(@Valid OnlyOne newOnlyOne, RedirectAttributes redirectAttributes)
        {
            onlyOneService.createOnlyOne(newOnlyOne);
            redirectAttributes.addFlashAttribute("message","Onlyne创建");
            return "redirect:/demo/onlyone/";
        }
         
        //删除
        @RequestMapping(value = "delete/{id}")
        public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes)
        {
            onlyOneService.deleteOnlyOne(id);
            return "redirect:/demo/onlyone/";
        }
         
        //修改
        @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
        public String update(@PathVariable("id") Long id, Model model)
        {
            model.addAttribute("onlyone",onlyOneService.getOnlyOne(id));
            model.addAttribute("action","update");
             
            return "demo/onlyone/update";
        }
         
        @RequestMapping(value = "update",method = RequestMethod.POST)
        public String update(@Valid OnlyOne updateOnlyOne,RedirectAttributes redirectAttributes)
        {
            onlyOneService.updateOnlyOne(updateOnlyOne);
            //TODO:没有实现i18n
            redirectAttributes.addFlashAttribute("message","Onlyone更新");
             
            return "redirect:/demo/onlyone/";
        }
         
        //查询
        private static final String PAGE_SIZE = "3";
        private static Map<String,String> sortTypes = Maps.newLinkedHashMap();
        static {
            //sortTypes.put("auto", "自动");
            //sortTypes.put("title", "标题");
        }
         
        @RequestMapping(method = RequestMethod.GET)
        public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
                           @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
                           @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model,
                           ServletRequest request) {
     
            //warning:search_ & reviews? is same thing
            Map<String, Object> searchParams = QueryRequestUtil.getParameter(request,"search_");
            //该demo为单一只有一个实体,不需要交叉查询与获取用户相关信息
            //Long userId = getCurrentUserId();
             
            System.out.println("page:" + pageNumber);
            System.out.println("page.size:" + pageSize);
            System.out.println("sortType:" + sortType);
     
            System.out.println("db begin");
            Page<OnlyOne> onlyOnes = onlyOneService.searchOnlyOnePage(searchParams, pageNumber, pageSize, sortType);
     
            System.out.println("db final");
             
            model.addAttribute("onlyones", onlyOnes);
            model.addAttribute("sortType", sortType);
            model.addAttribute("sortTypes", sortTypes);
            // 将搜索条件编码成字符串,用于排序,分页的URL
            model.addAttribute("searchParams", QueryRequestUtil.encodeParameter(searchParams, "search_"));
     
            return "demo/onlyone/list";
        }
    }
    
    2019-07-17 19:06:46
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
workshop专场-微服务专场-开发者动手实践营-微服务-Spring Cloud Alibaba 微服务全家桶体验 立即下载
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载

相关实验场景

更多