spring MVC 入门(实例一)创建简单springMVC

首先是Web.xml :

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	
	<servlet>
		<!-- SpringMVC的前端控制器 -->   
    		<!-- 当DispatcherServlet载入后,它将从一个XML文件中载入Spring的应用上下文,该XML文件的名字取决于<servlet-name> -->   
    		<!-- 这里DispatcherServlet将试图从一个叫做springmvc-servlet.xml的文件中载入应用上下文,其默认位于WEB-INF目录下 -->   
    		<!-- 所以ContextLoaderListener参数值也可写成<param-value>classpath:applicationContext-*.xml</param-value> -->   		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<!-- 使用ContextLoaderListener配置时,需要告诉它Spring配置文件的位置 -->   
    			<!-- 如果没有指定,上下文载入器会在/WEB-INF/applicationContext.xml中找Spring配置文件 -->   
    			<!-- 我们可以通过在Servlet上下文中设置contextConfigLocation参数,来为上下文载入器指定一个或多个Spring配置文件 -->   
    			<!-- 注意:contextConfigLocation参数是一个用逗号分隔的路径列表,其路径是相对于Web系统的根路径的 -->   
			<param-name>contextConfigLocation</param-name>
			<param-value>
				/WEB-INF/springmvc-servlet.xml,
				/WEB-INF/springmvc-view.xml
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<!--
		<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>WEB-INF/log4j.properties</param-value>
	</context-param>
	 -->
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


2.接着是 springmvc-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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="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">
 	<!--初始化控制器-->
	<bean name="TestServlet" class="com.huoli.servlet.TestServlet"></bean>
 </beans>


3.视图处理模型:

<?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:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="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">
 	
	<!-- 【配置视图解析器】 -->   
    	<!-- InternalResourceViewResolver会在ModelAndView返回的视图名前加上prefix指定的前缀,再在最后加上suffix指定的后缀 -->   
    	<!-- 由于TestServlet返回的ModelAndView中的视图名是message,故该视图解析器将在/WEB-INF/jsp/message.jsp处查找视图 -->
	<!-- 若Controller的方法返回"user/msg",则SpringMVC自动找/WEB-INF/jsp/user/msg.jsp -->
	<bean id="viewresolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
 	
 </beans>


4.控制器部分:TestServlet

@Controller
public class TestServlet{
	
	//将会映射到 http://Ip:port/ProjectName/getInfo,当请求getInfo时,将会被web.xml中的 DispacherServlet捕获并寻找合适的控制器处理,这里	//是getInfo方法 之后再交由合适的试图解析器进行展示。因为返回的model为  message 且配置文件中没有对该请求有专门的解析器,所以将会有	//viewresolver 进行解析 将该请求映射到  /WEB-INF/message.jsp文件进行展示
	@RequestMapping("getInfo")
	public ModelAndView getInfo(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView("message","msg","as大手大脚ask大家as的");
	}
	
	//将该请求映射到 /WEB-INF/m/m.jsp文件进行展示
	@RequestMapping("getInfo2")
	public String getInfo2(HttpServletRequest request, HttpServletResponse response){
		request.setAttribute("msg", "啊撒打算打算打哈就看到户籍卡黑色的金卡华盛顿号");
		return  "m/m" ;
	}
}

5.视图jsp文件:

WEB-INF/message.jsp:

<%@ page contentType="application/json; charset=UTF-8" %>

<%=request.getAttribute("msg")%>

 

/WEB-INF/m/m.jsp :

  <body>
  	<p>啊撒打算打算打接哈加快速度户籍卡圣诞节卡号是大家哈撒</p>
    <%=request.getAttribute("msg")%><br>
  </body>
</html>



 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值