小弟最近在用SpringMVC。网上的例子写得都比较清楚明白的,可小弟自己试试,却怎么也运行不起来!
着急啊,请求小伙伴们支援一下!!
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Spring MVC</display-name>
<!-- 定义webAppRootKey 防止多个工程冲突,主要由log4j引发 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>candy</param-value>
</context-param>
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>WEB-INF/logback.xml</param-value>
</context-param>
<listener>
<listener-class>com.yjf.common.log.LogbackConfigListener</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring配置 -->
<!-- ====================================== -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring/*.xml,
classpath*:spring/**/*.xml
</param-value>
</context-param>
<!-- java 专用 WEB SERVICE -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!-- ==========MVC 配========= -->
<servlet>
<servlet-name>candy</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>candy</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ico</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/html/error.html</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/html/error.html</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/html/error.html</location>
</error-page>
<welcome-file-list>
<welcome-file>/index.htm</welcome-file>
</welcome-file-list>
</web-app>
candy-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName">
<!-- 自动扫描的包名 -->
<context:component-scan base-package="net.yeah.zhyang.candy.web.test"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Controler类
net.yeah.zhyang.candy.web.test.HelloControler.java
package net.yeah.zhyang.candy.web.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld () {
String message = "Hello World, Spring 3.0!";
System.out.println(message );
return new ModelAndView ("hello", "message", message);
}
}
其他的也没啥了,页面访问
http://127.0.0.1:8080/hello.htm
后台输出日志:
2014-12-15 16:36:19.755 WARN [-8080-exec-1] PageNotFound:1108 No mapping found for HTTP request with URI [/hello.htm] in DispatcherServlet with name 'candy'
服务启动日志:
十二月 15, 2014 4:36:14 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'candy'
2014-12-15 16:36:14.647 INFO [-startStop-1] [/]:183 Initializing Spring FrameworkServlet 'candy'
2014-12-15 16:36:14.647 INFO [-startStop-1] DispatcherServlet:454 FrameworkServlet 'candy': initialization started
2014-12-15 16:36:14.655 INFO [-startStop-1] XmlWebApplicationContext:510 Refreshing WebApplicationContext for namespace 'candy-servlet': startup date [Mon Dec 15 16:36:14 CST 2014]; parent: Root WebApplicationContext
2014-12-15 16:36:14.660 INFO [-startStop-1] XmlBeanDefinitionReader:315 Loading XML bean definitions from ServletContext resource [/WEB-INF/candy-servlet.xml]
2014-12-15 16:36:14.737 INFO [-startStop-1] DefaultListableBeanFactory:598 Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3fbb173d: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,viewResolver,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@6e4a9d91
2014-12-15 16:36:15.218 INFO [-startStop-1] DispatcherServlet:473 FrameworkServlet 'candy': initialization completed in 570 ms
2014-12-15 16:36:15.260 INFO [ main] Http11Protocol:183 Starting ProtocolHandler ["http-bio-8080"]
十二月 15, 2014 4:36:15 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
请小伙伴们支援一下,小弟不胜感谢!!!