SSM-SpringMVC-SpringMVC开发流程详解-控制器的开发
控制器是SpringMVC核心内容,一般分三步:
- 获取请求参数
- 处理业务逻辑
- 绑定模型和视图
获取请求参数
通过@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;
}
}
这里只是完成了业务逻辑,并没有视图渲染