springMVC的一个demo

首先到引入jar包 ,懒得分类,直接把下载的spring所有都copy进去了,自己查查各个jar包都是干嘛的。

然后是web.xml文件

<display-name>test1</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!--添加spring  -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:spring/applicationContext.xml
		</param-value>
	</context-param>
	<!-- springMVC -->
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
				classpath:spring/springMVC.xml
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- 拦截请求 -->
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>

然后是applicationContext.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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 					http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 					http://www.springframework.org/schema/context
 					http://www.springframework.org/schema/context/spring-context-3.1.xsd
 					http://www.springframework.org/schema/mvc
					http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
					http://www.springframework.org/schema/aop
					http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
					http://www.springframework.org/schema/tx
					http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 
</beans>

然后是springMVC.xml,这才是springMVC的核心配置,demo不用太复杂,简单写写咯

<?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:p="http://www.springframework.org/schema/p"
 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-3.1.xsd
 					http://www.springframework.org/schema/context
 					http://www.springframework.org/schema/context/spring-context-3.1.xsd
 					http://www.springframework.org/schema/mvc
					http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    <!-- 启用spring mvc 注解 -->
    <context:annotation-config />
 
    <!-- 设置使用注解的类所在的jar包 -->
    <context:component-scan  use-default-filters="false" base-package="com.gy.controller">
    	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
 
    <!-- 完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 
    <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:suffix=".html" />
</beans>

好了,配置这些就够用了,然后是前端请求,后端处理了……

前端这样写……

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>springMVC demo</title>
</head>
<body>
	<form action="test/test.htm" method="post">
		<input type="text" name="name"/>
		<input type="submit" value="测试" />
	</form>
</body>
</html>

接受这样写…… 最后返回的是一个路径,test2.html

@Controller
@RequestMapping("/test")
public class Test {
	
	@RequestMapping("/test.htm")
	public String test(@RequestParam("name")String name) {
		System.out.println("测试通过 "+name);
		return "/test2";
	}
}

相关推荐