关闭

Spring MVC(一)实现Controller接口的控制器

136人阅读 评论(0) 收藏 举报
分类:
1.web.xml

	<!-- 总派发中心 -->
	<servlet>
		<servlet-name>test</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>test</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

2.StartController.java

package com.yw.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class StartController implements Controller
{

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception
	{
		System.out.println("controller is working");
		return new ModelAndView("start");
	}

}

3.test-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: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-2.0.xsd
				http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<!--HandleMapping根据 用户发出start.do的请求 找到对应的控制器Controller进行处理 -->
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="start.do">startController</prop>
			</props>
		</property>
	</bean>

	<!-- 控制器的实现类以及包 -->
	<bean id="startController" class="com.yw.controller.StartController"></bean>
	
	<!--视图解析器 加前缀和后缀,隐藏jsp,放在web-inf目录, -->
	<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<!-- viewResovle view 需要加,否则报错国际化时使用JstlView-->
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

</beans>



4.start.jsp(如图)




5.运行效果


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:240747次
    • 积分:3912
    • 等级:
    • 排名:第8060名
    • 原创:164篇
    • 转载:99篇
    • 译文:0篇
    • 评论:13条
    最新评论