springmvc创建的基础

本文详细介绍Spring MVC的配置步骤,包括web.xml设置、spring-mvc.xml配置文件详解及动态资源解析器使用,并通过具体实例展示如何实现控制器的请求处理与响应。

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

1、springmvc的web.xml配置

2、在web-inf下创建classes文件夹

3、创建spring-mvc.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"

   xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="

      http://www.springframework.org/schema/beans 

      http://www.springframework.org/schema/beans/spring-beans-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

      http://www.springframework.org/schema/aop 

      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

      http://www.springframework.org/schema/mvc

      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

      <!-- 组件扫描 -->

     <context:component-scan base-package="com.lanou.mvctest"></context:component-scan>

     <!-- 动态 全局资源试图解析器 -->

   <bean    class="org.springframework.web.servlet.view.InternalResourceViewResolver">

     <property name="prefix"value="/jsp/"></property>

     <property name="suffix" value=".jsp"></property>

   </bean>

     <!--注解驱动 -->

     <mvc:annotation-driven></mvc:annotation-driven>

     <!-- ************************* -->

     <!-- 静态通过资源处理 ,不会分给动态-->

     <mvc:resourceslocation="/static/"  mapping="/static/**"/>

</beans>

5、配置文件基本完毕

6、在webContent下创建jsp动态文件夹

static下方js 、css、.html文件

7、以下应用实例

package com.lanou.mvctest.controller;


import java.util.HashMap;

import java.util.Map;


import javax.annotation.Resource;


import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.servlet.ModelAndView;


import com.lanou.mvctest.entiry.User;

import com.lanou.mvctest.services.IUserService;

/**

 * struts的属性分装

 * springMVC基于参数封装

 *

 */

//视图解析器相当于struts2的result

@Controller

@RequestMapping("/user") //映射文件 namespace

public classUserController {

publicstatic  Loggerlog=Logger.getLogger(UserController.class);

//@Autowired //根据类型自动装配

@Resource(name="userService")//根据名字装配

publicIUserService userService;

@RequestMapping("/login")//接口

@ResponseBody//返回json 时配置

//返回值String类型  返回动态页面 //参数可以直接对象 

    publicMap<String,Object>login(@RequestParam(name="name")String username,Stringpwd){

log.info("请求到这里了");

System.out.println(username);

  Map<String,Object>  result=newHashMap<>();

  result.put("status",1);

  result.put("data","hahahahaha");

    return result;

    }

//参数可以是原生的servlet的内置对象

@RequestMapping("/test")

public String test(Modelmodel){

model.addAttribute("username","fuxionglongferqhjniogerqjmngioreqj'jpjn'");

return"user/bbb";

}

@RequestMapping("/test1")

publicModelAndView  test1(){

Map<String,Object>  map=newHashMap<>();

map.put("username","zhangkaikai");

ModelAndView modelAndView  =newModelAndView("user/bbb",map);

returnmodelAndView;

}

@RequestMapping("/service")

@ResponseBody

publicString testService(Useruser){

  userService.login(user);

return"success";

}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞腾创客

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值