您的位置:首页 > 编程语言 > Java开发

Spring MVC4.2.1 xml配置

2015-10-13 00:33 302 查看
有了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包的下载(好坑+不容易),调试(英文+浏览器+视频很重要)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: