Memulai proyek Web di Servlet 3.0 tidak dapat lagi memerlukan file konfigurasi Web.xml, sehingga konfigurasi dalam artikel ini hanya valid dalam wadah web yang mendukung Servlet 3.0 dan di atas
Penggunaan pegas MVC (4.3.2.release) + thymeleaf (3.0.2.release), jdbctemplate yang digunakan dalam lapisan persistensi. PS: Rekomendasikan kerangka kerja yang sangat berguna untuk enkapsulasi jdbctemplate: https://github.com/selfly/dexcoder-assistant. Berikut ini adalah konfigurasi spesifik:
Konfigurasikan Spring MVC DispatcherServlet
DispatcherServlet adalah inti dari Spring MVC. Spring menyediakan kelas AbstractAnnotationConfigDispatcherServletInitializer yang dengan cepat mengkonfigurasi DispatcherServlet. Kode spesifiknya adalah sebagai berikut:
di mana onstartup () adalah metode dalam antarmuka WebApplicationInitializer, dan pengguna mengkonfigurasi filter dan pendengar lainnya.
getrootconfigclasses () Mendapat kelas konfigurasi, apa yang saya pahami setara dengan konteks yang dibuat oleh applicationContext.xml
getServletConfigClasses () Mendapat kelas konfigurasi, yang setara dengan konteks yang dibuat oleh mvc-servlet.xml
Tidak ada komentar yang diperlukan dalam kategori ini
Paket com.liu.bank.config; impor org.springframework.web.webApplicationInitializer; impor org.springframework.web.filter.characterencodingFilter; impor org.springframework.web.servlet.support.abstractannationkotasionnasion javax.servlet.filterregistrasi; impor javax.servlet.servletcontext; impor javax.servlet.servletException; impor java.nio.charset.standardcharsets;/** * Pengguna: Liu * Tanggal: 2016-10-7 15:12 */Kelas Publik LIMPINICINZER PERLUHATAN PUBLICCATERIZER 2016-10-7 WebApplicationInitializer {@Override Protected Class <?> [] GetRootConfigClasses () {return class baru <?> [] {RootConfig.class}; } @Override Class Protected <?> [] GetSerVletConfigClasses () {return class baru <?> [] {Webconfig.class}; }/*** Mengkonfigurasi jalur agar sesuai dengan DispatcherServlet* @return*/@Override String Protected [] getServletMappings () {return new string [] {"/"}; } /** * Configure other servlets and filters * * @param servletContext * @throws ServletException */ @Override public void onStartup(ServletContext servletContext) throws ServletException { FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class); encodingfilter.setInitparameter ("encoding", string.ValueOf (StandardCharsets.utf_8)); encodingfilter.setinitparameter ("forceencoding", "true"); encodingfilter.addmappingForUrlPatterns (null, false, "/*"); }} Konfigurasikan ApplicationContext.xml, diimplementasikan oleh kelas RootConfig
Paket com.liu.bank.config; import com.mchange.v2.c3p0.compooledDataSource; impor org.springframework.context.annotation.*; impor org.spramework.core.env.vironment; impor org.springfrramework.core.env.environment; Impor org.springfrramework.jdbc. org.springframework.jdbc.datasource.datasourcetransactionManager; impor org.springframework.stereotype.controller; Impor org.springframework.transaction.platformransactionagager; impor org.springframework.transaction.annotasionenagagener; org.springframework.transaction.annotasionennaner; javax.annotation.Resource;import javax.sql.DataSource;import java.beans.PropertyVetoException;/** * User : liu * Date : 2016-10-7 15:36 */@Configuration@PropertySource("classpath:config.properties") // Import property file @EnableAspectJAutoProxy // Equivalent to <AOP: AspectJ-autoproxy/> @EnabletRansactionManagement in XML // Mengaktifkan transaksi anotasi @Componentscan (Basepackages = {"com.liulu.lit", "com.liulu.bank"}, excludefilters = @componentscan.filter (class = class = classion "}, excudefilters = @componentscan.filter (class = class = Diimpor di atas akan disuntikkan ke lingkungan @resource Private Environment Env; / ** * Mengkonfigurasi kumpulan koneksi basis data C3p0, * @return * @throws PropertyVetoException */ @Bean DataSource public DataSource () melempar PropertyVetoException {CombopooledDataSource dataSource = new CombooledDataSource (); DataSource.setjdbCurl (env.getProperty ("db.url")); DataSource.SetDriverClass (env.getProperty ("db.driver")); DataSource.SetUser (env.getProperty ("db.user")); DataSource.SetPassword (env.getProperty ("db.password")); DataSource.SetMinPoolSize (integer.valueof (env.getProperty ("pool.minpoolsize")))); DataSource.SetMaxPoolSize (integer.valueof (env.getProperty ("pool.maxpoolsize")))); DataSource.setAutocommitonclose (false); DataSource.setcheckoutTimeout (integer.valueof (env.getProperty ("pool.checkouttimeout"))); DataSource.setAcquireretryAttempts (2); Return DataSource; } / ** * Mengkonfigurasi hal -hal Manajer * @param DataSource * @return * / @bean Platform PublicTransActionManager TransactionManager (DataSource DataSource) {return new DataSourCetransActionManager (DataSource); } @Bean public jdbctemplate jdbctemplate (DataSource DataSource) {return new jdbctemplate (DataSource); }}File config.properties ada di direktori sumber daya
Konfigurasi #database db.url = jdbc: mysql: //192.168.182.135: 3306/bankdb.driver = com.mysql.jdbc.driverdb.user = rootdb.password = 123456#Data Connection#Connection#Pool Connection. Koneksi dicadangkan dalam koneksi kolam renang biliar.
Konfigurasikan servlet.xml, diimplementasikan oleh kelas webconfig
Konfigurasi Templat Thymeleaf juga di bawah
Paket com.liu.bank.config; impor org.springframework.context.annotation.bean; impor org.springframework.context.annotation. org.springframework.web.servlet.viewResolver; impor org.springframework.web.servlet.config.annotation.defaultServlethandlerConfigurer; import org.springframework.web.servlet.config.notation.eNableBmvcvc org.springframework.web.servlet.config.annotation.webmvcconfigurerAdapter; impor org.thymeleaf.templateengine; impor org.thymeleaf. org.thymeleaf.spring4.view.thymeleafviewResolver; impor org.thymeleaf.templateMode.templatemode; import java.nio.charset.standardcharsets;/** * enable: Liu * Tanggal 2016-10-7 15:16 */@Configuration@ENABER: Liu: Liu: 2016-10-7 15:16 */@ <mvc: anotasi-driven /> @componentscan (BasePackages = {"com.liulu.bank.controller", "com.liulu.lit"}, termasuk Falkcccilters = @componentscan.filter (class = controller.class), bekas. Sumber daya statis yang ditangani oleh wadah web, yang setara dengan <mvc: default-servlet-handler/> di xml */ @Override void public configuredefaultserVlethandling (konfigurasi DefaultSerVlethandlerConfigurer) {configurer.enable (); } / *** Tiga kacang berikut dikonfigurasi Templat thymeleaf* @return* / @bean springResourCetemplateresolver templateresolver () {springResourCetemplateresolver templateresolver = springresourcetemplateresolver (); templateresolver.setprefix ("/web-inf/template/"); templateresolver.setuffix (". html"); templateresolver.settemplatemode (templateMode.html); templateresolver.setcharacterencoding (string.valueof (StandardCharsets.utf_8)); Return Templateresolver; } @Bean TemplateEngine TemplateEngine (SpringResourCetemplateresolver templateresolver) {springTemplateEngine templateEngine = springtemplateEngine baru (); templateengine.settempleateresolver (templateresolver); kembalikan templateengine; } @Bean viewResolver viewResolver (templateengine templateengine) {thymeleafviewResolver viewResolver = thymeleafviewResolver () baru; viewResolver.settemplatEngine (templateengine); viewResolver.setcharacterencoding (string.valueof (StandardCharsets.utf_8)); Return ViewResolver; }}Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.