Fall 2018 web training log 0928

Posted by bazza84 on Sun, 21 Jun 2020 12:46:06 +0200


AOP programming:
Notification class, target class, enhanced code, weaving process, join point (pointcut)
Pre notice, post notice, final notice, exception notice
AOP can be used to isolate all parts of business logic, so as to reduce the coupling between all parts of business logic,
Improve the reusability of the program, and improve the efficiency of development.
The main functions are: logging, performance statistics, security control, transaction processing, exception handling and so on.
Annotation realizes AOP programming:
Before the target class:
@Component ("object name") creates an object for calling methods
Before the notification class:
@Component ("object name") creates an object for calling methods
@Aspect: indicates that the object of this class is a notification object
Before the corresponding function:
@Before (value = "exclusion (* pointcut path)"): pre notification
The last three after, afterreturn, after... Are similar
Scan comments in profile:
< context:component-scan base-package= "Notification class path" / >
Note effective label:< aop:sapectj-autoproxy >
Framework: Spring MVC
1)spring subframe
2) Using MVC design pattern to develop web layer (jsp, servlet)
3)MVC design pattern: realize layered development
5) Layered development:
C ontroller:servlet (receive user request, distribute request) -- > Service (process business logic) -- > Dao (database operation) -- > response -- > controller (call view) -- > View (display result)
6) Design principle:
-(user request) - > front end Controller (distribution request DispatchServlet) - > back end processor mapper (looking for back-end processor HandlerMapping) --- return the found back-end processor -- > front end Controller -- > back end processor adapter -- -- call -- > back end processor (processing user request Controller on the server) --- processing results (including data, view Figure) return -- > back end processor adapter ---- return processing result ----- front end Controller --- processing result ----- > View parser (rendering data to view) --- return view ----- > front end Controller -------- response to user


Code implementation:

1) Import jar package

2) To configure a front-end controller:( web.xml )

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
<!--to configure springMVC Front end controller-->
    <!--Load when using the front controller SpringMVC Profile for
    //Default search location / WEB-INF/servlet name- servlet.xml
    //You can also specify your own location -- >
        <!--Configure the front-end controller to intercept all requests-->

3) Spring MVC profile( mvccfg.xml ):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="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/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context
    <!--Configure mapper-->
    <!--BeanNameUrlHandlerMapping:If the requested path is the same as the name of the processor object, the mapping succeeds-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--Configure processor adapter-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!--Processor (write by yourself)-->
    <!--id The name must be the same as the request path-->
    <bean id="/user" class="com.hyx.handler.MyHandler"/>
    <!--Configure view resolver-->
    <!--The location of the response page needs to be configured-->
    <!--The location of the response page needs to be configured
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/JSP/"/>
        <property name="suffix" value=".JSP"/>

4) Processor class (error, caution)

package com.hyx.handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;

public class MyHandler implements Controller {
    public ModelAndView handlereRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws Exception{
        //Call service to complete business logic processing and dao to complete database operation
        //Data and views returned by the storage processor
        ModelAndView mv=new ModelAndView();
        //Save data
        return mv;

5) Entrance

<%-- Created by IntelliJ IDEA. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<a href="/user">Request data</a>
  <%--User request--%>

6) Response page

  Created by IntelliJ IDEA.
  User: Administrater
  Date: 2018/9/28
  Time: 15:32
  To change this template use File | Settings | File Templates.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <title>Query results</title>




Topics: xml Spring Java JSP