使用myeclipse构建maven管理的springmvc项目

1.创建一个maven webapp项目。
2.完善项目源文件夹
src/main/java,src/main/resources,src/test/java,src/test/resources.
3.更改项目字符集,右击项目,properties-resources.
4.更改web工程版本,进入myeclipse-project facets ,将dynatic web变为3.0.
5.修改发布目录,将Deployment Assembly里面的test删掉。将maven dependency 配置到 “WEB-INF/lib”,src/main/webapp配置到“/”,将src/main/java和src/main/resources配置到“WEB-INF/classes”
6.在pom中添加servlet,junit,spring-web,spring-webmvc依赖,
7.在src/main/java下编写配置类(例如在kai.config包中)

package kai.web.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpitWebAppInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class[] getRootConfigClasses() {
    // TODO Auto-generated method stub
    return new Class[]{RootConfig.class};
}

@Override
protected Class[] getServletConfigClasses() {
    // TODO Auto-generated method stub
    return new Class[]{WebConfig.class};
}

@Override
protected String[] getServletMappings() {
    // TODO Auto-generated method stub
    return new String[]{"/"};
}

}


8.在kai.web.config包中编写俩个应用上下文RootConfig和WebConfig。


package kai.web.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan(basePackages={“kai.web.service”},excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})
public class RootConfig {

}

package kai.web.config;

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(“kai.web.controller”)
public class WebConfig extends WebMvcConfigurerAdapter{
@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) {
// TODO Auto-generated method stub
configurer.enable();
}
}


9.然后就可以编写控制器来实现功能了。
在kai.web.controller包下创建一个新的控制器来试试。

package kai.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
@RequestMapping(value=”/”,method=RequestMethod.GET)
public String home() {
// TODO Auto-generated method stub
return “home”;
}
}

10.最后在WEB-INF/views/下编写一个home.jsp就行了。
访问localhost:8080/springdemo页面显示:
使用myeclipse构建maven管理的springmvc项目_第1张图片
参考文章:
https://wenku.baidu.com/view/52f177980975f46527d3e1dc.html

你可能感兴趣的:(web)