Fall 2018 web training log 0928

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

Morning:

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
4)M:MODEL V:VIEW C:CONTROLLER
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

afternoon:

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"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
           version="4.0">
<!--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 -- >
<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:mvccfg.xml</param-value>
    </init-param>
</servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
        <!--Configure the front-end controller to intercept all requests-->
    </servlet-mapping>
</web-app>

3) Spring MVC profile( mvccfg.xml ):

<?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: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
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--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
    prefix:/WEB-INF/JSP/
    suffix:.JSP
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/JSP/"/>
        <property name="suffix" value=".JSP"/>
    </bean>
</beans>

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 {
    @Override
    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
        mv.addObject("RESULT","tom");
        mv.setViewName("result");
        return mv;
    }
}

5) Entrance

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

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" %>
<html>
<head>
    <title>Query results</title>
</head>
<body>
//Query RESULT: ${RESULT}
</body>
</html>

 

 


 

Topics: xml Spring Java JSP