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

同样作为初学者,在这里希望能帮到同样问题的童鞋,也鼓励你不要遇到问题就放弃!