2018用IDEA搭建SSM框架(Spring+SpringMVC+Mybatis) (3)

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 " version="4.0"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--加载Spring容器--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <!--加载Spring监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--springMVC前端控制器加载--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--contextConfigLocation配置SpringMVC加载的配置文件(配置处理器,映射器等等) 如果不配置contextConfigLocation,默认加载的是:/WEB-INF/servlet名称-servlet.xml(springmvc-servlet.xml) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <!-- 1. /* 拦截所有 jsp js png .css 真的全拦截 建议不使用 2. *.action *.do 拦截以do action 结尾的请求 肯定能使用 ERP 3. / 拦截所有 (不包括jsp) (包含.js .png.css) 强烈建议使用 前台 面向消费者 /对静态资源放行 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--spring提供 乱码过滤器--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> SSM框架就此已经搭建完成,下面进行测试 mybatis进行逆向工程

生产po、mapper相关文件,看图。

如何通过mybatis逆向工程生成,见链接。后续更新

测试类 mybatis框架测试

在src/test/java下面进行创建包com.flyme.service,在此下面进行创建CourseServiceImplTest测试类,以用来测试mybatis框架是否搭建成功。

image

CourseServiceImplTest.java package com.flyme.service; import com.flyme.po.Course; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CourseServiceImplTest { private ApplicationContext applicationContext; @Before public void setUp() throws Exception { applicationContext = new ClassPathXmlApplicationContext(new String[]{"spring/applicationContext-dao.xml", "spring/applicationContext-service.xml"}); } @Test public void findById() throws Exception { CourseService courseService = (CourseService) applicationContext.getBean("courseServiceImpl"); Course course = courseService.findById(1); System.out.println(course.toString()); } }

运行findById()方法,出现以下图,证明搭建成功

image

SpringMVC框架测试 AdminController.java

在src/main/java,com.flyme.controller包下面创建AdminController.java,以用来测SpringMVC框架是否搭建成功。

image

package com.flyme.controller; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/myController") public class AdminController extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("name"); req.setAttribute("name", name); System.out.println(name); req.getRequestDispatcher("index.jsp").forward(req, resp); } } idex.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="myController" method="post"> <input> return:${name} <input value="提交" type="submit"> </form> </body> </html>

运行项目

配置Tomcat

image

image

image

image

最后选择OK,即可。

浏览器输入 :8010/ (因为笔者改动了端口号,灵活应用)

输入:“我是”

image

结果:

image

配置成功 问题集锦

如有问题欢迎评论
...

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgwps.html