在Spring MVC中没有参数的@RequestMapping

Ume*_*til 6 java spring spring-mvc

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

在spring中,@RequestMapping注释需要参数.例如,

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

我的项目使用注释,它不使用任何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";
    }
Run Code Online (Sandbox Code Playgroud)

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

有什么用@AutoWired

Thu*_*yen 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";
    }
    
    Run Code Online (Sandbox Code Playgroud)

    请求参数和标头值

    @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
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    使用@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";
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    @RequestBody使用注释映射请求正文

    @RequestMapping(value = "/something", method = RequestMethod.PUT)
    public void handle(@RequestBody String body, Writer writer) throws IOException {
        writer.write(body);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 自动装配

    @Autowired
    private RuleStepService ruleStepService;
    
    Run Code Online (Sandbox Code Playgroud)

    Spring容器之前已经创建了bean ruleStepService。如果你需要在你的类中使用这个bean,你只需要像上面那样声明,容器就会将该bean注入到你的类中。你不需要像这样声明;

    RuleStepService ruleStepService =new RuleStepService(). 
    
    Run Code Online (Sandbox Code Playgroud)

    容器将查找bean名称ruleStepService或bean的类型RuleStepService(基于配置中的策略)