基于maven的ssm搭建的完整配置(二)最终篇

本文是基于maven的SSM项目搭建的第二部分,详细介绍了从`spring-service.xml`配置到SpringMVC层,再到Controller和视图层的编写。包括首页`index.jsp`和书籍列表页面`allbook.jsp`的创建。最后,讲解了如何配置Tomcat进行运行,确保项目可以正常启动和测试。

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


第一部分

看这里 —> 基于maven的ssm搭建(一)


spring-service.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"
        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">

    <!--1.扫描service下的包-->
    <context:component-scan base-package="com.jasomWu.service"></context:component-scan>

    <!--2.将所有的业务类,注入到spring,可以通过配置或注解。这里用了注解-->
<!--    <bean id="BookServiceImpl"-->
<!--          class="com.jasomWu.service.BookServiceImpl">-->
<!--        <property name="bookMapper" ref="bookMapper"/>-->
<!--    </bean>-->

    <!--3.声明式事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--4.aop事务支持-->

</beans>

SpringMVC层

web.xml

<?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_4_0.xsd"
         version="4.0">
    <!--DispatcherServlet-->
    <servlet>
        <servlet-name>springmvc</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>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>

有需要可以配上去乱码过滤

<!--乱码过滤-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

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:mvc="http://www.springframework.org/schema/mvc"
        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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置SpringMVC -->
    <!--1.注解驱动-->
    <mvc:annotation-driven/>

    <!--2.静态资源过滤器-->
    <mvc:default-servlet-handler/>

    <!--3.扫描包:controller-->
    <context:component-scan base-package="com.jasomWu.controller"/>

    <!--4.视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/page/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

        <import resource="spring-dao.xml"></import>
        <import resource="spring-service.xml"></import>
        <import resource="spring-mvc.xml"></import>
</beans>

Controller 和 视图层编写

@Controller
@RequestMapping("/book")
public class BookController {
    //调用service层
//    @Autowired
//    @Qualifier("bookServiceImpl")
    @Resource(name = "bookServiceImpl")
    private BookService bookService;

    //查询全部书籍
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Book> bookList = bookService.queryAllBooks();
        model.addAttribute("books",bookList);
        return "allBook";
    }

    @RequestMapping("/test")
    public String test(Model model){
        String str = "测试专用!!";
        model.addAttribute("test",str);
        return "test";
    }
}

编写首页 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首页</title>
  </head>
  <body>
    <a href="${pageContext.request.contextPath}/book/allBook">进入书籍展示页面</a><br/>
    <a href="${pageContext.request.contextPath}/book/test">测试项</a><br/>
  </body>
</html>

书籍列表页面 allbook.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>书籍展示</title>
    <!-- 引入 Bootstrap -->
    <link
        href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
        rel="stylesheet">
</head>
<body>

<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                    <h1 style="color: darkkhaki">书籍列表————————————所有书籍</h1>
            </div>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-hover table-striped">
                <thead>
                    <td>编号</td>
                    <td>书名</td>
                    <td>作者</td>
                    <td>价格</td>
                    <td>销量</td>
                    <td>库存</td>
                </thead>
                <thead>
                <c:forEach items="${books}" var="book">
                    <tr>
                        <td>${book.id}</td>
                        <td>${book.name}</td>
                        <td>${book.author}</td>
                        <td>${book.price}</td>
                        <td>${book.sales}</td>
                        <td>${book.stock}</td>
                    </tr>
                </c:forEach>
                </thead>
            </table>
        </div>
    </div>
</div>


</body>
</html>

配置Tomcat,进行运行!

在这里插入图片描述
到目前为止,这个SSM项目整合已经完全的OK了,可以直接运行进行测试
在这里插入图片描述
在这里插入图片描述
OK ,到这里,能够坚持到这里的各位都是人才,说话有好听,敲的代码又好看,我超喜欢你们,所以,我要是哪里写错了,欢迎指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值