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

阅读更多

 

1、新建一个maven project;

 

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


	
  4.0.0
  com.huatech
  web-config-demo
  0.0.1
  war
  
  
  		
			org.springframework
			spring-webmvc
			4.2.5.RELEASE
		
		
		
			javax.servlet
			javax.servlet-api
			3.0.1
			provided
		
		
  
  
  
  	
  		
			org.apache.maven.plugins
			maven-compiler-plugin
			3.6.0
			
				1.8
				1.8
				UTF-8
			
		
  		
  		
  			org.apache.maven.plugins
  			maven-war-plugin
  			2.6
  			
  				false
  			
  		  		
  	
  

 

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", [email protected](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", [email protected](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 (9.1 KB)
  • 下载次数: 2

你可能感兴趣的:(java,web)