Spring MVC4.2.1 xml配置

有了jsp+servlet的基础,自然而然,SpringMVC的 基本的web.xml 配置就比较简单。以下为我的第一个搭载Spring MVC的基础项目的有关代码:

基础的web.xml配置:注意一般哪个版本的Spring的jar包,都可以使用旧版本Spring的jar包相关的web.xml的基础配置,如

 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>test2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
           <servlet-name>test2</servlet-name>
           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
           <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
           <servlet-name>test2</servlet-name>
           <url-pattern>*.test2</url-pattern>
  </servlet-mapping>

</web-app>

Spring-servlet.xml配置:

beans(对象工厂的定义 对于哪个版本的Spring都是通用的,虽然我用的是最新版的)

<?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"
             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.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
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
   
         <property name="mappings">
           <props>
               <prop key="start.test2">startController</prop>
           </props>
         </property>
    </bean>
    
    <bean id="startController" class="test.StartController"></bean>
</beans>

接下来便是controller的代码,主要是为了第一次接触SpringMVC的概念而写的:

package test;

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 {
		// TODO Auto-generated method stub
		System.out.println("success");
		System.out.println("success too");
		System.out.println("success too too");
		return null;
	}

}

至此,当控制台输出:

success
success too
success too too

我的SpringMVC尝试便成功了。。

当然我的过程少不了jar包的下载(好坑+不容易),调试(英文+浏览器+视频很重要)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值