spring: spittr实例 构建简单的web应用
我的环境是: jdk8, spirng4
之前照者书上说的做了,不得成功,于是网上百度,不得其然。
后来看到一篇文章,甚是所感。https://segmentfault.com/q/1010000007921684/a-1020000007922842
单词拼错了,无地自容……
我的包名是spittr
但是我的自动扫描写的是spitter, 晕,忘了给视图解析器加@Bean了 或者很多注解写成一排了
于是乎鼓捣重来
SpittrWebAppInitializer
package spittr.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
//根容器
return new Class<?>[] { RootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
//Spring mvc容器
return new Class<?>[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
//DispatcherServlet映射,从"/"开始
return new String[] { "/" };
}
}
RootConfig
package spittr.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration
@ComponentScan(
basePackages = {"spittr"},
excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)
}
)
public class RootConfig {
}
WebConfig
package spittr.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("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter{ @Bean
public ViewResolver viewResolver() { //配置JSP视图解析器
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
//可以在JSP页面中通过${}访问beans
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
} @Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable(); //配置静态文件处理
}
}
homeController
package spittr.web; 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() {
return "home";
} }
目录如下:

运行结果:

spring: spittr实例 构建简单的web应用的更多相关文章
- spring: spittr实例 构建简单的web应用 Test测试用例
本例为Test,测试上一贴的程序 package spittr.web; import org.junit.Test; import org.springframework.test.web.serv ...
- maven构建简单的web项目
把jdk给换掉 项目修改好了以后写个页面测试一下,结果正常 下面应该添加依赖让web项目一步步丰满起来. 0-添加依赖 1-建一个servlet 2-web.xml中添加servlet声明 3-重新运 ...
- 用Spring MVC开发简单的Web应用
这个例子是来自于Gary Mak等人写的Spring攻略(第二版)第八章Spring @MVC中的一个例子,在此以学习为目的进行记录. 问题:想用Spring MVC开发一个简单的Web应用, 学习这 ...
- 使用spring 4.0 + maven 构建超简单的web项目
一.需求 使用spring去管理web项目,是目前非常流行的一种思路,本文将介绍使用maven+spring 4.0.2 来构建一个简单的web项目. 二.实现 1.新建一个maven项目,如下图所示 ...
- idea+Spring+Mybatis+jersey+jetty构建一个简单的web项目
一.先使用idea创建一个maven项目. 二.引入jar包,修改pom.xml <dependencies> <dependency> <groupId>org. ...
- Spring学习(五)--构建Spring Web应用程序
一.Spring MVC起步 看过猫和老鼠的小伙伴都可以想象Tom猫所制作的捕鼠器:它的目标 是发送一个小钢球,让它经过一系列稀奇古怪的装置,最后触发捕鼠 器.小钢球穿过各种复杂的配件,从一个斜坡上滚 ...
- [译]Spring Boot 构建一个RESTful Web服务
翻译地址:https://spring.io/guides/gs/rest-service/ 构建一个RESTful Web服务 本指南将指导您完成使用spring创建一个“hello world”R ...
- 用Spring MVC开发简单的Web应用程序
1 工具与环境 借助Eclipse4.3 + Maven3.0.3构建Java Web应用程序.使用Maven内置的servlet 容器jetty,不需手工集成Web服务器到Eclipse.还帮我们自 ...
- Spring WebFlux 教程:如何构建反应式 Web 应用程序
Spring WebFlux 教程:如何构建反应式 Web 应用程序 反应式系统提供了我们在高数据流世界中所需的无与伦比的响应能力和可扩展性.然而,反应式系统需要经过专门培训的工具和开发人员来实现这些 ...
随机推荐
- window下使用mysql,报未定义标识符"SOCKET"
解决方法一: 这个错误是在VC中使用MySQL数据库时出现在mysql_com.h文件中的 my_socket fd; 说明未my_socket未定义,这时只需要在引用mysql.h头文件之前引用# ...
- luarocks错误 require ‘luasql.mysql' 报module 'luasql.mysql' not found:
错误: require 'luasql.mysql'stdin:1: module 'luasql.mysql' not found: no field package.preload['luasql ...
- JS去遍历Table的所有单元格中的内容
用JS去遍历Table的所有单元格中的内容,可以用如下JS代码实现 function GetInfoFromTable(tableid) { var tableInfo = ""; ...
- java final 关键词
package day9; /** * Created by admin on 2018/11/17. * final可以修饰类,方法,变量 特点: final可以修饰类,该类不能被继承. final ...
- 利用Docker快速部署Oracle环境
工作中需要频繁使用Oracle环境,但是每次搭建起来比较消耗时间,本想通过虚拟机模板的方式来快速安装oracle vm,但是每次改ip等环境也很耗时,因此想到docker中有没有已经做好的images ...
- django settings相关配置
settings """ Django settings for mysite project. Generated by 'django-admin startproj ...
- MySQL数据库Date型数据插入问题
MySQL数据库中,Date型数据插入问题,总是提示如下错误: “java.util.Date cannot be cast to java.sql.Date” 解决办法: 1.首先,获取Date型数 ...
- Nodejs学习计划
此文章已经发表于本人博客. 由于公司要求这段时间在学习Nodejs,基本靠自摸一路走来踩了很多坑浪费很多时间,今天就来这里说下,顺便计划一下接下来的学习计划,目前自己做个博客,项目过程中学习了js类以 ...
- 转:MVC遇上bootstrap后的ajax表单验证
使用bootstrap后他由他自带的样式has-error,想要使用它就会比较麻烦,往常使用jqueyr.validate的话只有使用他自己的样式了,而且有模型在使用模型验证更方便点.怎么解决呢? 当 ...
- Poj 1755Triathlon 未Ac,先mark
地址:http://poj.org/problem?id=1755 题目: Triathlon Time Limit: 1000MS Memory Limit: 10000K Total Subm ...