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

springMVC框架入门

2015-11-05 16:30 549 查看
步骤一:开发环境

            开发工具:myeclipse/eclipse

              jdk:1.7       tomcat 7.0

             spring相关jar包  可自行在百度搜索下载

步骤二:建立动态web工程,结构投入下,仅供参考:



步骤三:相应的java类和配置文件如下:

1.Java文件controller入口:

package com.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 HelloWorldController implements Controller{

@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {

System.out.println("spring MVC test successful !");

return new ModelAndView("/index");
}

}

2.web.xml配置文件:

配置系统DispatcherServlet:
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/SpringMVC-servlet.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>
</web-app></span>


3.SpringMVC-servlet.xml配置文件:

    注意xml文件名对应关系:因为web.xml定义servlet-name为SpringMVC,所以这个文件名必须为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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="/springMVCTest/helloworld" class="com.controller.HelloWorldController"></bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>


4.jsp文件index.jsp
   做一个最后VIEW层的展示返回,可自行定义

步骤四:启动tomcat,输入地址:http://localhost:8080/SpringMVC/springMVCTest/helloworld  结果如下:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  springMVC入门