This is the 11th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Just for the sake of common sense, our JavaWeb program starts with a web.xml file

Break down

For common sense purposes, our JavaWeb program starts with a web.xml file. This is our core file. We can’t run a project without a web.xml file.

web.xml

  • <display-name> Name of the project </display-name>

  • Normally we will configure some filter, which means that the specification processing before the execution of an event can continue. Our common filters are encoding filter, data security filter. A common coding filter is attached below

` ` ` <! Utf-8 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ```Copy the code
  • When you open a file with a browser, you will find that if it is a doc file, you will open it with notepad, but if you configure the type in web.xml, the browser will automatically call the corresponding program to open it according to the configured type

Configure common file types in web.xml

```
<mime-mapping>  
<extension>doc</extension>  
<mime-type>application/vnd.ms-word</mime-type>  
Copy the code
  • These things help with configuration, and after that, it’s time to configure the framework, which is basically a servlet, so we need to reference servlets to configure the framework. We’re throughcontext-paramLoad the Spring configuration file
` ` ` <! <context-param> < context-name >contextConfigLocation</ context-name >contextConfigLocation</ context-name > <param-value>classpath:applicationContext.xml</param-value> </context-param> <! Spring configuration file --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ```Copy the code
  • With the configuration files loaded, we are left to launch the framework
``` <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <! -- You can customize the location and name of the servlet. XML configuration file. The default is in the WEB-INF directory and the name is [<servlet-name>]-servlet. XML. Such as spring - servlet. XML < init - param > < param - name > contextConfigLocation < / param - name > <param-value>/WEB-INF/spring-servlet.xml</param-value>&nbsp; </init-param> <param-name>contextConfigLocation</param-name> contextConfigLocation</param-name> contextConfigLocation</param-name> contextConfigLocation</param-name> contextConfigLocation</param-name> <param-value>classpath:config/springmvc.xml</param-value> </init-param> <! -- <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> ```Copy the code
  • The whole framework is configured, and we are configuring some error interfaces in the web.xml file to make the whole system more user-friendly.
- Jump according to the error codeCopy the code
<error-page>  
       <error-code>403</error-code>  
       <location>/change/error_go.action</location>  
</error-page>
Copy the code
- Jump according to the error typeCopy the code
<! This configuration means that if a JSP page or servlet has an Exception of type Java.lang. Exception (including subclasses, of course), it will go to the 500.jsp page for processing. --> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/change/error_go.action</location> </error-page>Copy the code
  • Finally, the welcome page of the system

<welcome-file-list>
		<welcome-file>change/login_go.action</welcome-file>
	</welcome-file-list>
Copy the code

Application. XML configuration file

Enable annotation configuration

<! <context:component-scan base-package="tm.change"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>Copy the code
  • Configuring a Data source
<!-- 配置C3P0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${c3p0.driverClass}"></property>
		<property name="jdbcUrl" value="${c3p0.url}"></property>
		<property name="user" value="${c3p0.user}"></property>
		<property name="password" value="${c3p0.password}"></property>

	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:config/mybatis-config.xml" />
	</bean>
Copy the code

Of course, you need to introduce the configuration of the database before you use this, which is basically the properties file, where you put the data connection information

  • Mybatis’ Mapper interface (which can be seen as dao layer processing)
<! - configure the processing of scanning mapper interfaces - > < bean class = "org. Mybatis. Spring. Mapper. MapperScannerConfigurer" > < property name = "basePackage" value="tm.change.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>Copy the code
  • Transaction processing (think of it as the Service layer)
- Register a transaction <! -- -- -- > configuration transactions < bean id = "myTxManager" class = ". Org. Springframework. JDBC datasource. DataSourceTransactionManager "> < property Name ="dataSource" ref="dataSource" /> </bean> - configure transaction notification (specify which methods will have transaction management) <! <tx:advice ID ="txAdvice" transaction-manager="myTxManager"> <tx:attributes> <! <tx:method name="insert*" Propagation ="REQUIRED" Isolation ="DEFAULT" /> <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> ``` - Pointcut (which has transactions in it) <! <aop:config> <aop:pointcut expression="execution(* tm.change.service.. * (..) )" id="txPointcut" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> </aop:config> <context:annotation-config /> ```Copy the code

Insert *, Update *, DELETE * and so on have transactions on the service layer

  • The Controller layer (springmVC.xml file)
- Config view resolver <! - the parser configuration view - > < bean class = "org. Springframework. Web. Servlet. The InternalResourceViewResolver" > <! -- prefix -- > < property name = "prefix" value = "/ WEB - INF/" / > <! -- suffix - > < property name = "suffix" value = "JSP" / > < / bean > ` ` `Copy the code
  • Set the view parser for uploading files
` ` ` <! <bean id="multipartResolver" > class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" Value ="20971520" /> </bean> - Configure the interceptor to intercept all requests directly, or customize the interceptor path < MVC :interceptors> <! - define intercept all requests directly - > < bean class = "com. Wxisme. SSM. Interceptor. IdentityInterceptor" > < / bean > <! -- < MVC :interceptor> Intercepts all path requests including subpath < MVC :mapping path="/**"/> <bean class="com.wxisme.ssm.interceptor.IdentityInterceptor"></bean> </mvc:interceptor> --> </mvc:interceptors> ```Copy the code
  • Configure some bindings such as date bindings
` ` ` <! <bean id="conversionService" > class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <list> <! - date type binding - > < bean class = "com. Wxisme. SSM. Controller. The converter. The DateConverter" / > < / list > < / property > < / bean > ` ` `Copy the code
  • Static resources are not loaded.

This is normal, but some people will set springMVC’s blocking address to any address. If the url is set as follows, then only.action requests will be intercepted. Then static resources will be intercepted. At this time, our system will not be able to load static resources and display the interface

```
<servlet-mapping>
	<servlet-name>springmvc</servlet-name>
	<url-pattern>*.action</url-pattern>
</servlet-mapping>
```
Copy the code

Isn’t there anything we can do about that? Yes, look at the code

` ` ` <! -- Access static resource files -->Copy the code

<mvc:resources mapping=”/images/” location=”/images/” /> <mvc:resources mapping=”/css/” location=”/css/” />

<mvc:resources mapping=”/js/” location=”/js/” /> <mvc:resources mapping=”/imgdata/” location=”/imgdata/” /> “`