Spring Beginners 9 --- Spring MVC using annotation processing

Record what you project the bug, toss a long time to get.

When using intllij build SpringMVC project, sometimes you encounter problems can not download jar package, there will be error "Failed to download 'http://central.maven.org/maven2/org/springframework/springaop/4.3.18.RELEASE /spring-aop-4.3.18.RELEASE.jar':Request failed with status code 501 ", initially thought it was because of my heavenly network is limited, but reported that 501, there is no other information, and then in web site to test the connection, you see, already has recommended, the next operation is the meal, look for a variety of configurations, hoping to modify the request path out, ultimately did not get rid of, the internet has never found a similar situation, then Put another way, using maven project to create this springmvc

 

 Use mavn create mvc webapp remember to choose that package, because we have to request from the network, then after entering the project, re-added to the mav spring mvc framework, the increase or decrease in intllij right frame, if there is no spring mvc, then go to project struct in the spring already deleted, the incomplete, the right to re-select, add frame spring mvc.

Having bug, we entered, now let's look at the use of annotation, note that we are now talking about the situation before and after the end together, that is coupled with the continuous development of business, the company will have basically the front and rear end engineer , engineer in charge of the back-end processing logic, and returns the corresponding data, but we look at some of the projects have included the rendered view, this can help us learn more.

First look at some of the basic grammar notes

@Controller

This bean is responsible for registering to spring context, can scan the frame is spring, almost like component usage Logically speaking, but I measured a bit, the interface can not access it later changed to component

@RequestMapping

This is to inform the processor that this method can handle requests Url, if this method is written in the class, it is equivalent to the foregoing method url build on a path based on the class

@RequestParam

This is to the rear end of the preceding parameters parameters decoupled, that is, when a request comes in, the capture request parameters, then the corresponding field value is passed

There are three parameters to this comment

  • value: Specifies the name of the property, which is the field preceding the request
  • required: specify whether the field must be passed
  • defaultValue: Set the default value of this field, which is equivalent to get the python method to obtain a value, if not get, give a default value

Then simply use these notes we look at the project configuration, first look engineering Screenshot

 

web.xml do not change, we look directly dispatcher-servlet file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--加载扫描器,并制定包-->
    <context:component-scan base-package="controller" />

    <!--Because the method jsp file WEB-INF outside, can be requested directly by the path to, so this time, we have to move the file to the WEB-INF jsp inside, then take some simple configuration for easy to find in the view -> 
    < bean the above mentioned id = "viewResolver" 
          class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > 
        <-! this is an increase of prefixes -> 
        < Property name = "prefix" value = "/ the WEB-INF / Page / "  /> 
        <! - this is to increase the suffix, we can not write when loading view controller in .jsp -> 
        < Property name =" suffix " value . =" JSP "  /> 
    </ bean > 
</beans>

Look at a map to help us better understand the role of this viewResolver

 

 Next we see a kind of control is to see authenticate

package controller;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller  // Controller就是通知Spring框架进行扫描的,我这个试了一下,用Component就无法访问,具体原理后续再研究
@RequestMapping("/index")  // 在类前面加这个注解,就会给下面所有方法前面都加上这个路由
public class Authenticate {
    // 这个就是路由
    @RequestMapping("/authenticate")
    // @RequestParam 就是我们说到的可以给参数起别名,这样如果前段字段名称发生变化,就只需要修改一处,可以指定
    public ModelAndView handleRequest(@RequestParam(value = "username", required = true, defaultValue = "hong") String u, @RequestParam("password") String p) throws Exception {
        // 看这个,是不是就不再需要我们写前缀跟后缀了,这个即使在despatcher-servlet中配置的
        ModelAndView mav = new ModelAndView("result");
        String message = "认证失败";
        if (u.equals("ming") && p.equals("1234")) {
            message = "认证成功";
        }
        // 告知要向视图添加对象
        mav.addObject("message", message);
        // 返回我们设置好的mav
        return mav;
    }
}

这个就是简单的注解使用,今天调编辑器浪费了挺多时间,先到这吧。成长就是再踩坑的路上不断前行。

Guess you like

Origin www.cnblogs.com/yangshixiong/p/12203829.html