快速搭建一个无web.xml的web工程(二)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 快速搭建一个无web.xml的web工程(二)

快速搭建一个无web.xml的web工程(二)

 2017/12/22 0:56:18  hbxflihua  程序员俱乐部  我要评论(0)
  • 摘要:1、新建一个mavenproject;2、在pom.xml中引入spring-webmvc<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0
  • 标签:Web 一个 XML

?

1、新建一个maven project

?

2、在pom.xml中引入spring-webmvc

class="xml" name="code"><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();
	}
	
}

?

  • web-config-demo.zip (6.4 KB)
  • 下载次数: 0
上一篇: RabbitMQ在windows下的安装 下一篇: 没有下一篇了!
发表评论
用户名: 匿名