spring mvc环境搭建

原理不说了,貌似直接是基于servlet实现的,直接上配置文件吧

web.xml

复制代码
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <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>
</web-app>
复制代码

注:

url-pattern表示拦截的url,*.do表示拦截所有以do结尾的请求,
如果配置为/可以实现restful,但是会影响静态资源的处理

spring-servlet.xml配置
复制代码
<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.rest" />
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><!--
        处理rest返回时中文乱码
        -->
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                            <value>text/plain;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/" p:suffix=".jsp" />
</beans>
复制代码

demo

复制代码
package com.mvc.rest;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/HelloWorld")
public class HelloWorldController {
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String hello() {

        return "/welcome";
    }

    @RequestMapping(value = "/myhello", method = RequestMethod.GET)
    public ModelAndView myhello() {
        ModelMap modelMap = new ModelMap();
        modelMap.put("name", "吴继芳");
        modelMap.put("age", 20);
        List<String> list = new ArrayList<String>();
        list.add("code");
        list.add("game");
        list.add("read");
        list.add("study");
        modelMap.put("list", list);
        return new ModelAndView("/myhello", modelMap);
    }

    /**
     * session操作
     * 
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/mySession", method = RequestMethod.GET)
    public ModelAndView mySession(HttpSession httpSession) {
        ModelMap modelMap = new ModelMap();
        UUID uuid = UUID.randomUUID();
        String a = uuid.toString();
        httpSession.setAttribute("si", a);
        modelMap.put("si", httpSession.getAttribute("si"));
        return new ModelAndView("/mysession", modelMap);
    }

    @RequestMapping(value = "/rest", produces = "text/plain;charset=UTF-8")
    public @ResponseBody
    String rest() {
        return "你好!rest";
    }

    // @RequestMapping(value = "/rest{id}", produces =
    // "text/plain;charset=UTF-8")
    @RequestMapping(value = "/rest{id}", produces = "text/plain;charset=UTF-8")
    public @ResponseBody
    String restT(@PathVariable int id) {
        return "你好!rest" + id;
    }
    
    /**
     * @return
     */
    @RequestMapping(value="rt",produces = "text/plain;charset=UTF-8")
    public @ResponseBody
    String rt() {
        return "你好!rest";
    }
    
}
复制代码

 




 

posted @   wujf  阅读(205)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示