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 能解决吗?
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";
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。