SpringMVC组件解析

本文阅读 1 分钟
首页 代码,Java 正文

img img

img

1.value

注意:value通常可以省略。

@RequestMapping(value = "/quick")

2.method

@RequestMapping(value = "/quick",method = RequestMethod.GET)

3.params

@RequestMapping(value = "/quick",method = RequestMethod.GET,params = { "userName"})

4.案例

结合:Spring入门案例: 详细代码 只需要修改: UserControll:

package com.study.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/user")
public class UserController { 
    @RequestMapping(value = "/quick",method = RequestMethod.GET,params = { "userName"})
    public String save(){ 
        System.out.println("UserController....");
        return "/jsp/success.jsp";
    }
}

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <!-- 配置SpringMVC前端控制器-->
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

测试: 报错: img 解决办法: 输入?userName=xxx img

img

<!--Controller的组件扫描-->
    <context:component-scan base-package="com.study.controller"></context:component-scan>

img spring-mvc.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:context="http://www.springframework.org/schema/context"
       xmlns:http="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
    <!--Controller的组件扫描-->
    <context:component-scan base-package="com.study.controller"></context:component-scan>
    <!--配置内部资源视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

UserController

package com.study.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/user")
public class UserController { 
    @RequestMapping(value = "/quick",method = RequestMethod.GET,params = { "userName"})
    public String save(){ 
        System.out.println("UserController....");
        return "success";

    }
}

测试: img

img

本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/qq_43514330/article/details/125468422
-- 展开阅读全文 --
安全面试之XSS(跨站脚本攻击)
« 上一篇 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复