Spring 梳理 - JavaConfig实战(spring MVC)-原创
2024-09-01 10:05:41
- 目录结构
- AppInitializer.java
package com.jt; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
} @Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
} @Override
protected String[] getServletMappings() {
return new String[] { "/" };
} }
- RootConfig.java
package com.jt; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration
public class RootConfig { }
- WebConfig.java
package com.jt; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration
@EnableWebMvc
@ComponentScan("com.jt")
public class WebConfig extends WebMvcConfigurerAdapter {
public WebConfig(){
System.out.println("WebConfig");
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
} @Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
- HelloControl.java
package com.jt; import java.sql.Date;
import java.util.Map; import javax.validation.Valid; import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod.*; import com.myOrg.DeptUserDomain; @Controller
@RequestMapping(value = "/FirstControl")
public class HelloControl {
@RequestMapping(value="/registerDeptUser",method=RequestMethod.GET)
public String register(){ return "registerDeptUser";
}
}
- registerDeptUser.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post">
id:<input type="text" name="id" /><br /> name:<input type="text"
name="name" /><br /> <input type="submit" value="submit" />
</form>
</body>
</html>
- 运行
- 参考:https://blog.csdn.net/bufanqi_info/article/details/78922534
最新文章
- log4net 记录日志到sqlserver
- 02 Apache Solr: 概览 Solr在信息系统架构中的位置
- Docker - Docker国内镜像的配置及使用
- zTree的使用2
- Linux下查看文件内容的命令
- 【C#】【Thread】Monitor和Lock
- 【原创】Quartz代码详解
- spoj 2319 BIGSEQ - Sequence
- Js打开新窗口拦截问题整理
- 201521123031 《Java程序设计》第13周学习总结
- [lua][openresty]代码覆盖率检测的解决方式
- 微信小程序picker的坑
- Django 序列化-token
- 使用 jquery.wordexport.js导出的Word排版
- 【javascript小案例】从0开始实现一个俄罗斯方块
- io系列之其他类
- SAP CRM 忠诚度相关表的关系图
- Unable to cast COM object of type &#39;Shell32.ShellClass&#39; to interface type &#39;Shell32.IShellDispatch6&#39;.
- Vue2.5开发去哪儿网App 首页开发
- python-day47--mysql数据备份与恢复