2014-07-22 58 views
5

我想了解我的項目代碼,同時學習Spring MVC。Spring MVC中沒有參數的@RequestMapping

在春季@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有什麼用?

+1

不帶任何參數是在一個方法中使用,則它使用從GET/POST請求中識別輸入參數的方法的參數。例如。在你的'ruleStepEntForm'方法中,GET/POST中的參數應該被命名爲'populationId' – Pat

回答

3
  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. 自動裝配

    映射請求體

    Spring Container之前創建了豆ruleStepService。如果你需要在你的類中使用這個bean,你只需要像上面那樣聲明,容器就會把這個bean注入到你的類中。你不需要申報;

    RuleStepService ruleStepService =new RuleStepService(). 
    

    集裝箱會發現bean名稱ruleStepService或豆具有類型RuleStepService(基於配置的策略)

    @RequestMapping時
0

控制器中使用的@RequestMapping註釋允許我們不僅實現URL模式匹配,而且還從請求URL本身提取變量。因此,您必須擁有@RequestMapping,無論您是否有參數。默認情況下,HTTP通過@RequestMapping映射方法是GET

這裏是從彈簧源2名良好的引用:

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/

+1

因此,給定OP的代碼,哪個URI(GET請求)模式將由'ruleStepOrgCount'方法處理? – Raedwald

+1

沒有附加映射的GET方法被映射到控制器映射本身的根目錄。 –

+1

我知道。但是在這種情況下'@ Controller'本身沒有'@ RequestMapping'。那麼,它的* URI模式是什麼? – Raedwald

相關問題