Spring MVC入门级实例-基于xml的配置

最近在学习spring MVC,为了留作以后参考,同时也让初学者能很快的入门,特意作了如下教程,闲话少说,上代码

我采用的是spring3.1.0.M2版本

 

1.web.xml配置

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
	  </context-param>
	 <listener>
	        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	 </listener>
	  
     <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
      <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

 

 2.创建一个*-servlet.xml,*代表web配置的servlet-name,该文件默认放在web.xml同级目录里,主要配置映射处理器和视图解析器,springMVC-servlet.xml配置如下

  

 <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>  
        <property name="suffix" value=".jsp"/>
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
     </bean>  

 

3.applicationContext.xml配置如下

   

 <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
     <mvc:annotation-driven />
        
        <!--通过url中的method参数指定要执行的controller方法-->
       <bean id="methodNameResolver"
		class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
		<property name="paramName">
			<value>method</value>
		</property>
		<property name="defaultMethodName">
			<value>view</value>
		</property>
	</bean>
     
   <bean name="/index.html" class="com.ldh.mvc.noannotation.IndexController"/>
       
    <bean name="/user/operation.html" class="com.ldh.mvc.noannotation.InternalPathController">
        <property name="methodNameResolver" ref="methodNameResolver"></property>
    </bean> 

 

 一定要加  <mvc:annotation-driven />配置

 

4.controller文件

  (1). 实现Controller接口,此时不需要通过url指定执行的方法,默认执行handleRequest方法

 

public class IndexController implements Controller{
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		System.out.println("Hello index=================");
		Map<String,String> resultMap = new HashMap<String,String>();
		resultMap.put("message", "欢迎来到首页");
		return new ModelAndView("/index", resultMap);
	}
}

 

 (2).实现MultiActionController接口,此时可以通过url指定执行的方法,controller里可以有多个执行方法

 

public class InternalPathController extends MultiActionController{
	
	public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,String> resultMap = new HashMap<String,String>();
		resultMap.put("message", "消息更新成功");
		return new ModelAndView("/page/update", resultMap);
	}
	
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,String> resultMap = new HashMap<String,String>();
		resultMap.put("message", "消息删除成功");
		return new ModelAndView("/page/delete", resultMap);
	}
}

 

ModelAndView构造方法的第一个参数指定文件路径,"/page/delete"即跳到根路径下的page目录下的delete.jsp页面

5.访问路径

   http://localhost/index.html

   http://localhost/user/operation.html?method=update

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值