您的位置:首页 > 编程语言 > Java开发

基于java代码的Spring-mvc框架配置

2016-12-01 20:43 471 查看
Spring 版本 4.3.2 maven项目

1.首先上项目目录图,主要用到的配置文件,略去css和js的文件



引包:



2.主要代码:

(1)NetpageWebAppInitializer类

package config;

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

/**
*@author 作者  Yu chenchen
*@version 创建时间:2016年12月1日 下午4:46:20
*类说明:该类会自动的配置DispatcherServlet和spring应用上下文,spring的应用上下文会位于应用程序的Servlet上下文之中
*/
public class NetpageWebAppInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {

//将DispatcherServlet映射到"/",处理所有的请求
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[]{"/"};
}
//返回的带有@Configuration注解的类将会用来配置ContextLoaderListener创建的应用上下文中的bean
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[]{RootConfig.class};
}
//返回带有@Configuration注解的类将会用来定义DispatcherServlet应用上下文中的bean
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[]{WebConfig.class};
}
}


(2)WebConfig类

package 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;

/**
*@author 作者  Yu chenchen
*@version 创建时间:2016年12月1日 下午5:13:20
*类说明:
*/
@Configuration
@EnableWebMvc  //启用Spring MVC
@ComponentScan(basePackages={"web"})//自动扫描包web下的所有控制器
public class WebConfig extends WebMvcConfigurerAdapter {

//配置jsp视图解析器
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver=new InternalResourceViewResolver();
resolver.setPrefix("/jsp/");//设置视图路径
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
//配置静态资源的处理
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
// TODO Auto-generated method stub
configurer.enable();
super.configureDefaultServletHandling(configurer);
}
}


(3)RootConfig类

package 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;

/**
*@author 作者  Yu chenchen
*@version 创建时间:2016年12月1日 下午4:55:11
*类说明:
*/
@Configuration
@ComponentScan(basePackages={"config"},
excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)}
)

//配置非web的组件,通常是后端的中间层和数据层组件
public class RootConfig {

}


(4)HomeController类

package web;

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

/**
*@author 作者  Yu chenchen
*@version 创建时间:2016年12月1日 下午5:39:53
*类说明:
*/
@Controller
public class HomeController {

//配置对"/"的请求
@RequestMapping(value="/",method=GET)
public String goHome(){
return "home";//返回视图名
}
}


(5)home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="css/test1.css">
</head>

<!--toolbar-->
<body>
<div id="toolbar" class="ss">
<div class="s0">
<div  class="login" >
<div class="logo"><img src="img/toolbar_logo.png"></img> </div>
<input class="s1" placeholder="输入账号">
<input class="s2" >
<input type="button" class="button1" value="登陆">
<a href="#" class="s3">注册</a>
</div>
<div class="email"> <a  href="#"  >邮件</a></div>
<div class="email"> <a  href="#"  >说两句</a></div>
<div class="s4"> <a href="#"><img src="img/sohunewsapp2.jpg"></a></div>
</div>
</div>
<!-- 主要页面 -->
<div class="main">
<!-- 第一行部分 导航-->
<div class="nav"></div>

<!-- 第二部分  搜索栏-->
<div class="search"></div>

<!-- 第三部分  新闻内容-->
<div class="content"></div>
</div>
</body>
<script type="text/javascript" src="js/test1.js"></script>
</html>


注意事项:将项目根路径映射到webapp下,不然会找不到资源。



3. 测试

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: