SSM-SpringMVC-SpringMVC开发流程详解-控制器的开发

本文详细介绍了SpringMVC控制器的开发流程,包括如何通过@RequestParam注解获取请求参数,处理业务逻辑以及模型和视图的绑定。首先,讲解了如何使用@RequestParam注解从HTTP请求中获取参数,并讨论了其配置选项。接着,展示了通过XML配置文件实现数据库连接和Mybatis的集成,以及如何在控制器中注入并调用服务层方法处理业务。最后,虽然业务逻辑已完成,但并未涉及视图的渲染部分。

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

SSM-SpringMVC-SpringMVC开发流程详解-控制器的开发

​ 控制器是SpringMVC核心内容,一般分三步:

  1. 获取请求参数
  2. 处理业务逻辑
  3. 绑定模型和视图

获取请求参数

通过@RequestParam注解方式获取:

@Request\Mapping(value="/index2",method=RequestMethod.GET)
public ModeAndView index2(@RequestParam("id") Long id){
    System.out.println(id);
    ModeAndView mv=new ModeAndView();
    mv.setViewName("index");
    return mv;
}

通过这种方式SpringMVC就知道从HTTP请求中获取参数,然后使用类似下面的转换既可获取参数:

String isStr=request.getParameter("id");
Long id=Long.parseLong(isStr);

注意:在默认情况下@RequestParam要求参数不能为空,如果需要,需要配置required是一个布尔值,设置false既可为空,还有defaultValue通过配置修改你想要的内容


实现逻辑和绑定视图

​ 实现的逻辑和数据库有关联,可以选择使用XML,注解,java配置的方法进行绑定配置。

XML配置:applicationContext.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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/aop
       http://www.springframework.org/schema/aop/spring-beans.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!--启动注解驱动-->
    <context:annotation-config></context:annotation-config>

    <!--数据库连接池-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://lovalhost:3306/ssm"/>
        <property name="username" value="root"/>
        <property name="password" value="123456789"/>
        <property name="maxTotal" value="255"/>
        <property name="maxWaitMillis" value="10000"/>
        <property name="maxIdle" value="5"/>
    </bean>

    <!--继承Mybatis-->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"/>
    </bean>

    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--采用自动扫描方式创建mapper bean-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ssm"/>
        <property name="sqlSessionFactory" ref="SqlSessionFactory"/>
        <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
    </bean>

</beans>

​ 假设上面XML配置文件,通过扫描的方式初始化一个SpringIOC容器中的Bean,RoleService提供一个参数为long的方法getRole获取角色,通过自动装配方式在控制前中注入:

@Controller
@RequestMapping("/role")
public class RoleControllor(){
    //注入角色服务类
    @Autowired
    private RoleService roleServive=null;
    
    @RequestMapping(value="/getRole",method=RequestMethod.GET)
    public ModeAndView getRole(@RequestParam ("id") Long id){
        Role role=roleService.getRole(id);
        ModeAndView mv=new ModeAndView();
        mv.setViewNAme("roleDetails");
        //给数据模型添加一个角色对象
        mv.addObject("role",role);
        return mv;
    }
}

​ 这里只是完成了业务逻辑,并没有视图渲染

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值