在Spring MVC中没有参数的@RequestMapping

时间:2014-07-22 11:18:34

标签: java spring spring-mvc

我在学习Spring MVC时试图理解我的项目代码。

在春天@RequestMapping注释需要参数。例如,

@RequestMapping(value="/something", method=RequestMethod.POST)
@RequestMapping(value="/index.html", method=RequestMethod.GET)
@RequestMapping("/index")
@RequestMapping(params="command=GETINFO")

我的项目使用注释,并且它不使用任何XML进行映射。我有一个以下的控制器结构。

@Controller
public class RuleStepController {
   private static final String ATTRIBUTE_BRANCH = "branch";
    private static final String ATTRIBUTE_EDIT_FORM = "editForm";
 .................
    @Autowired
    private RuleStepService ruleStepService;
    @Autowired
    private PopulationDao populationDao;

     @RequestMapping
    public void ruleStepEntForm(Long populationId, ModelMap model) {
     .....
     editForm.setStepEnt(stepDto);
    }

@RequestMapping
    public void ruleStepOrgCount(RuleStepOrgSearchForm searchForm, ModelMap model){
      .......
model.addAttribute("searchForm", searchForm);
    }

@RequestMapping
    public String ruleStepMgrForm() {
        logger.debug(String.format("ruleStepMgrForm"));
        return "forward:/employee/employeeSearchForm.view?relationshipId=0&roleId=0&formId=stepMgr";
    }

我想了解@RequestMapping什么时候没有任何参数会有什么意义?

@AutoWired的用途是什么?

2 个答案:

答案 0 :(得分:4)

  1. 使用注释@RequestMapping,您可以通过多种方式绑定请求参数:

    URI模板模式,使用注释@PathVariable

    @RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
    public String findOwner(@PathVariable String ownerId, Model model) {
        Owner owner = ownerService.findOwner(ownerId);
        model.addAttribute("owner", owner);
        return "displayOwner";
    }
    

    请求参数和标头值

    @Controller
    @RequestMapping("/owners/{ownerId}")
    public class RelativePathUriTemplateController {
    
        @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params = "myParam=myValue")
        public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
            // implementation omitted
        }
    }
    

    使用@RequestParam

    @Controller
    @RequestMapping("/pets")
    @SessionAttributes("pet")
    public class EditPetForm {
    
        @RequestMapping(method = RequestMethod.GET)
        public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
            Pet pet = this.clinic.loadPet(petId);
            model.addAttribute("pet", pet);
            return "petForm";
        }
    }
    

    使用@RequestBody注释

    映射请求正文
    @RequestMapping(value = "/something", method = RequestMethod.PUT)
    public void handle(@RequestBody String body, Writer writer) throws IOException {
        writer.write(body);
    }
    
  2. 自动装配

    @Autowired
    private RuleStepService ruleStepService;
    

    Spring Container之前创建了bean ruleStepService。如果你需要在你的类中使用这个bean,你只需要如上所述声明,容器会将bean注入你的类。你不需要声明像;

    RuleStepService ruleStepService =new RuleStepService(). 
    

    Container将找到bean名称ruleStepService或bean具有类型RuleStepService(基于配置中的策略)

答案 1 :(得分:0)

控制器中使用的@RequestMapping注释不仅允许我们实现URL模式匹配,而且还允许从请求URL本身提取变量。因此,您必须拥有@RequestMapping,您是否有参数。默认情况下,HTTP方法由@RequestMapping映射的是GET

以下是来自泉源的两个很好的参考资料:

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html

自动装配

这是有用的链接

http://howtodoinjava.com/2013/05/08/spring-beans-autowiring-concepts/