spring mvc 框架简易示例

本文介绍了一个基于SpringMVC的简单应用实例,包括web.xml和spring-servlet.xml的配置方式,以及通过@Controller注解实现的RESTful风格的控制器。演示了如何处理JSON请求和响应,并展示了使用反射调用不同类的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:p="http://www.springframework.org/schema/p"  
       xmlns:mvc="http://www.springframework.org/schema/mvc"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       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.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.mvc.test" />

    <mvc:annotation-driven/>

<!--防止IE下JSON被下载-->
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
        </bean>

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

控制器

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mvc.test;

import java.lang.reflect.Method;
import java.util.Hashtable;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author ly
 */
@Controller
public class HelloController {

    private Hashtable<String, String> _listMethod = new Hashtable<String, String>();

    @RequestMapping("test.do")
    @ResponseBody
    public String test() {
        return "dddd";
    }

    @RequestMapping(value = "tttt.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String writeJson(String test) {

        _listMethod.put("test1", "com.dll.test.TestDll");
        _listMethod.put("test2", "com.dll.test.TestReflect");

        Class<?> demo = null;
        try {
            String clsName = _listMethod.get(test);
            demo = Class.forName(clsName);
            Method method = demo.getMethod("getTest",String.class);
            Object instance = demo.newInstance();//創建實例
            Object object=method.invoke(instance,"arg test");
            String result =object.toString();
            return result;
        } catch (Exception e) {
            return e.getMessage().toString();
        }
    }
}

示例类

package com.dll.test;

/**
 *
 * @author ly
 */
public class TestReflect {
     public String getTest(String test){
        return "TestReflect中的方法getTest()";
    }
}
package com.dll.test;

/**
 *
 * @author ly
 */
public class TestDll {
    public String getTest(String test){
        return "testDll中的方法getTest(" + test+")";
    }
}

访问地址:
http://localhost:8080/mvcTest/tttt.do?test=test1
http://localhost:8080/mvcTest/tttt.do?test=test2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值