首页 > 学院 > 开发设计 > 正文

springMVC框架(一)HelloWold

2019-11-08 02:20:55
字体:
来源:转载
供稿:网友

公司使用的框架是jfinal,开发工具用的NetBeans,最近开始倒腾sPRingMVC了,就写了下Helloworld,中间还遇到了一个坑嗲的问题,那就是No mapping found for HTTP request with URI [/springmvc-01/] in DispatcherServlet with name 'dispatcherServlet'。

在网上找了很多资料,web.xml中配置的错误,<url-pattern>/*</url-pattern>改为/,把*去掉,但是很明显,我用的就是/。

后来挨个看配置文件也没有问题,项目启动的时候就是访问不到index.jsp

       仔细看了下,我的index.jsp放到了WEB-INF下面了,所有才访问不到的,index.jsp是和WEB-INF同级的。估计这个超低级的错误,只有我犯过吧。。

--------------------------------------------------------------------------------------------------------------------------------------------------

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"	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"	id="WebApp_ID" version="2.5">	<!-- 配置 DispatcherServlet -->	<servlet>		<servlet-name>dispatcherServlet</servlet-name>			<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>		<init-param>			<param-name>contextConfigLocation</param-name>			<param-value>classpath:dispatcherServlet-servlet.xml</param-value>		</init-param>	<load-on-startup>1</load-on-startup>	</servlet>	<servlet-mapping>		<servlet-name>dispatcherServlet</servlet-name>		<url-pattern>/</url-pattern>	</servlet-mapping></web-app>dispatcherServlet-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"	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">	<!-- 配置自定扫描的包 -->	<context:component-scan base-package="com.kevin.controller"></context:component-scan>	<!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">		<property name="prefix" value="/WEB-INF/views/"></property>		<property name="suffix" value=".jsp"></property>	</bean>	<!-- 在实际开发中通常都需配置 mvc:annotation-driven 标签 -->	<mvc:annotation-driven></mvc:annotation-driven></beans>HelloWorld.class

package com.kevin.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloWorld {	/**	 * 使用@RequestMapping来映射URL	 * 返回值会被视图解析器解析成物理试图,试图解析器会做如下解析	 * prefix+return值+后缀,得到实际视图,然后做转发操作	 * @return	 */	@RequestMapping("/helloworld")	public String hello(){		System.out.println("HelloWorld");		return "success";	}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表