快速搭建一个java config(无web.xml)的web工程(二)
程序员文章站
2022-07-13 09:07:19
...
1、新建一个maven project;
2、在pom.xml中引入spring-webmvc
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.huatech</groupId> <artifactId>web-config-demo</artifactId> <version>0.0.1</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
3、编写RootConfig,配置@Component扫包路径
package com.huatech.support; import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Controller; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; @Configuration @ComponentScan(basePackages="com.huatech", excludeFilters=@ComponentScan.Filter(type=FilterType.ANNOTATION, classes=Controller.class)) public class RootConfig { }
4、编写ServletConfig,配置Controller扫包路径
package com.huatech.support; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; @Configuration @ComponentScan(basePackages="com.huatech", includeFilters=@ComponentScan.Filter(type=FilterType.ANNOTATION, classes=Controller.class)) public class ServletConfig { }
5、编写WebAppInitializer,等价于web.xml
package com.huatech.support; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{RootConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{ServletConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
6、简单写一个Controller
package com.huatech.domain; import java.util.Date; public class User { private int id; private String username; private String password; private String email; private Date createTime; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + ", createTime=" + createTime + "]"; } }
package com.huatech.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.huatech.domain.User; @Controller public class UserController { @RequestMapping(value="/user/addPage", method = RequestMethod.GET) public String addPage(){ return "/user/addPage.jsp"; } @RequestMapping(value="/user/doAdd", method = RequestMethod.POST) @ResponseBody public String doAdd(User user){ return user.toString(); } }