eclipse+maven+spring 简单示例
来源:互联网 发布:软件开发就业岗位 编辑:程序博客网 时间:2024/06/03 18:08
网上关于环境搭建的文章一抓一大把,这里不再赘述!
这里主要解释下,tomcat启动后为什么报404的原因!
>>答案在下面,你把配置内容换下就不会有问题了。Mark一下,万一什么时候用得着呢对吧
1. 项目文件结构
主要关注一下 红色框 中的标注。
2. 配置
2.1 web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="study" version="2.5"> <display-name>Archetype Created Web Application</display-name> <description>Family tree construction</description> <!-- 加载Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/configs/spring-*.xml</param-value> </context-param> <!-- Spring监听 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring MVC配置 --> <servlet> <servlet-name>Dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 自定义spring mvc的配置文件名称和路径 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:configs/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- spring mvc 请求后缀 --> <servlet-mapping> <servlet-name>Dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
2.2 /configs/spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <context:annotation-config/> <context:component-scan base-package="hi.dubbo.sample.controller" /> <mvc:annotation-driven /> <mvc:default-servlet-handler/> <mvc:resources mapping="/styles/**" location="/styles/" /> <mvc:resources mapping="/scripts/**" location="/scripts/" /> <mvc:resources mapping="/images/**" location="/images/" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
2.3 spring注释
@Controller, @RequestMapping都是spring注解.
package hi.dubbo.sample.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class GeneralController {@RequestMapping("/hello")public ModelAndView hello() {ModelAndView mv = new ModelAndView();mv.addObject("attribute1", "Hello World!");mv.setViewName("hello");return mv;}}
2.4 jsp文件
index.jsp - web.xml有指定welcome-file为index.jsp.
hello.jsp - GeneralController.hello()方法中有指定viewName("hello"))
hello.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 http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Spring hello</title><style type="text/css">p {color: red;font-size: 20px;}</style></head><body><p>${attribute1}</p></body></html>
3. 单击666
同样作为初学者,在这里希望能帮到同样问题的童鞋,也鼓励你不要遇到问题就放弃!
阅读全文
0 0
- eclipse+maven+spring 简单示例
- Maven + Spring 简单示例
- SSH学习1:Maven + Spring 简单示例
- Maven+Eclipse+Spring MVC简单实例
- eclipse创建简单maven+spring项目
- Eclipse创建WEB工程Maven+Spring+SpringMVC+MyBatis示例
- 使用Maven构建Spring MVC项目的简单示例
- 使用Maven构建Spring MVC项目的简单示例
- 使用Maven构建Spring MVC项目的简单示例
- eclipse创建简单maven+spring项目(无xml)
- Eclipse使用maven搭建简单spring boot application Hello World
- eclipse 创建maven 项目示例
- Eclipse+maven+spring
- eclipse + maven + cxf +spring
- eclipse maven spring
- spring Aop简单示例
- Spring timer简单示例
- spring简单过滤器示例
- LeetCode: Add Two Number(已解原先出现的问题)
- Python的构造函数和析构函数 对象的变量和类的变量不一样
- spring整合redis
- fabirc1.0商业正式版本源码解析12——peer的MSP服务
- React 虚拟DOM及diff算法
- eclipse+maven+spring 简单示例
- 406. Queue Reconstruction by Height
- JVM虚拟机
- 13.java和c++区别(补充)
- 这是你想象中的Spring吗?
- 广西2017邀请赛 E: CS Course &,|,^ 运算
- 【codevs 4069】24点游戏
- 回归(regression)
- 正则验证