使用ssm框架做一个简单的网站

ssm开发网站(最基础的开发流程)

xml文件配置(Spring配置文件以及SpringMVC配置文件)
Spring配置文件

<?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: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-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- 使用注解驱动 -->
    <context:annotation-config />
    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/你的数据库名?useUnicode=true&amp;characterEncoding=UTF-8" />
        <property name="username" value="你的数据库用户名" />
        <property name="password" value="你的数据库密码" />
        <property name="maxActive" value="255" />
        <property name="maxIdle" value="5" />
        <property name="maxWait" value="10000" />
    </bean>

    <!-- 集成mybatis -->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:./com/Mybatis/Mybatis.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.*" />
        <property name="sqlSessionFactoryBeanName" value="SqlSessionFactory" />
        <property name="annotationClass" value="org.springframework.stereotype.Repository" />
    </bean>
</beans>

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:p="http://www.springframework.org/schema/p"
       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-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- 使用注解驱动 -->
    <mvc:annotation-driven />
    <!-- 定义扫描装载的包 -->
    <context:component-scan base-package="com.*" />
    <!-- 定义视图解析器 -->
    <!-- 找到Web工程/WEB-INF/JSP文件夹,且文件结尾为jsp的文件作为映射 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
    <!-- 如果有配置数据库事务,需要开启注解事务的,需要开启这段代码 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

Mybatis配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <mappers>
        <mapper resource="这里用来填写你的mapper映射文件地址,例如:com/Mapper/StudentMapper.xml"/>
    </mappers>
</configuration>

在ssm开发中一般使用注解的方式来对Controller、DAO以及MyBatis的接口进行注明,同时也要在spring和springMVC的配置文件中声明使用注解驱动。使用@Controller来注解控制层,控制层使用@RequestMapping来规定某个控制层方法用于处理指定的路径请求。一个简单的Controller层代码如下:

@Controller//表示这是一个Controller层的类
public class ChangeJSPController {
    @RequestMapping("/index")//表示该方法用于处理请求路径为./index.do的请求

    public ModelAndView changeJSP1(){

        ModelAndView mv = new ModelAndView();

            mv.setViewName("index");//设置返回的视图路径,由于在SpringMVC文件中配置了视图解析器viewResolver,因此在该请求结果返回到dispatchServlet以后会交给视图解析器处理,视图解析器对已了前缀为/WEB-INF/jsp/,后缀为.jsp的文件,因此会找到/WEB-INF/jsp文件夹下的index.jsp视图界面。


        return mv;
    }

文件目录结构如下:
这是一个完整项目的图,本次用不着所有文件
在对配置文件进行了配置以及简单的给出了控制层代码以后,(在进行下一步以前需要自己建立一个jsp文件放在WEB-INF下面,并且在新建的jsp文件加下新建index.jsp文件)将此程序运行于Tomcat服务器之上,打开浏览器输入localhost:8080/你的程序的名称/index.do就可以访问到你刚才所编写的index,jsp文件了。(第一次写博客,可能写的不好,请大家多多原谅,多多提意见!☺)

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值