SpringMVC(简单入门)

本文介绍了SpringMVC的基本配置方法,包括web.xml与SpringMVC配置文件的设置,讲解了@Controller、@RequestMapping等注解的应用,并演示了如何通过SpringMVC处理请求参数。

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

总结一下最近学习的SpringMVC的知识

  • web.xml与SpringMVC配置文件
  • 一些基本的注解

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
         version="3.1">

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

在web.xml文件中配置servlet:org.springframework.web.servlet.DispatcherServlet
是SpringMVC为我们提供的,在这里拦截了所有的请求。
applicationContext.xml文件则是我们的SpringMVC的配置文件。

<?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"
       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">
        <bean name="Product" class="app01.Product"/>
        <!-- 配置自动扫描的包-->
        <context:component-scan base-package="app01.main"/>

        <!-- 配置视图请求-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/views/"/>
                <property name="suffix" value=".jsp" />
        </bean>
</beans>

SpringMVC配置文件中添加:xmlns:context=”http://www.springframework.org/schema/context”
引入这个才能调用:
扫描我们存放Controller的包。


@Controller类

Controller类是我们的控制器,简单的说,就是处理我们求情的地方。

@RequestMapping(value = “/xxxx”)

@RequestMapping,这个注解主要用来对应的URL请求。
如果其中只有URI一个参数的话,可以不用写value(默认参数),如果有多个参数则必须写出来如
@RequestMapping(value=”/xxxx”,method =”RequestMethod.POST”)。
@RequestMapping(value=”/xxxx”,method ={“RequestMethod.POST”,”RequestMethod.PUT”})。

@Controller
public class Test{
    @RequestMapping("/xxx")
    public String method01(){
        //do some thing...
        return "ooooo";
    }
}

@RequestMapping(…)
也可以用来修饰类

@Controller
@RequestMapping("/book")
public class Test{
    @RequestMapping("/EnglishBook")
    public String method01(){
        //do some thing...
        return "ooooo";
    }
}//这里访问的路径则是 /book/EnglishBook

处理参数

调用servlet API
@RequestMapping("/uri")
public String method(HttpSession session){
    session.setAttribute(key,value);
    ...
}//HttpRequest 当然也行。。。
接收参数
@Controller
public class Test {
    @RequestMapping(value="/helloworld" parms="username","age")
    public String Hello(){
        System.out.println("hello world");
        return"success";
    }
}
使用@RequestParm绑定请求参数
@RequestMapping("/testparm")
public String Test(@RequestParm(value="username") String un,
@RequestParm(value="age",required=false) Integer age){
        System.out.println("username:"+un);
}
//required 设置是否是一定要带的参数
POJO对象绑定请求参数值

假定一个USER对象
private String name;
private String password;
private Address address; //级联属性

假定一个Address对象
private String provice;
private String city;

假定一个form表单

<form action="/test" method="post">
    name:<input type="text" name="name"/>
    password:<input type ="password" name="password">
    city:<input type="text" name="address.city/>
    privice:<input type="text" name="address.name"/>
</form>
@RequestMapping("/test")
public String method(User user){
    //可以直接接收到user对象啦。。。
}

@Autowired和@Service进行依赖注入

SpringMVC毕竟属于Spring的一部分,Spring框架的好处就是容易进行依赖注入。

如果需要类能被作为依赖注入,类必须注明为@Service。Service注释类型指示类是一个服务。另外需要在SpringMVC配置文件中需要添加一个:

<context: component-scan base-pakage="dependencyPackge">
public class product{
    @Autowired
    private productService service;
}

@Service
public class productService{
    ...
}

先记录这些吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值