ssm整合之编写SpringMVC框架

it2025-11-12  10

ssm整合之编写SpringMVC框架

1.在webapp/WEB-INF/web.xml配置的代码如下:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--配置前端控制器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载springmvc.xml配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!--启动服务器,创建该servlet--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--解决中文乱码的过滤器--> <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

2.在resources目录创建springmvc.xml的代码如下:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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 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"> <!--开启注解扫描,只扫描Controller注解--> <context:component-scan base-package="com.txw"> <!--只扫描这个注解--> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!--配置的视图解析器对象--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/WEB-INF/pages/"/> <!--后缀--> <property name="suffix" value=".jsp"/> </bean> <!--过滤静态资源--> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/images/" mapping="/images/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <!--开启SpringMVC注解的支持--> <mvc:annotation-driven/> </beans>

3.编写index.jsp的代码如下:

<%-- Created by IntelliJ IDEA. User: Adair Date: 2020/7/8 0008 Time: 14:26 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <a href="account/findAll">测试</a> </body> </html>

4.编写帐户的控制类的代码如下:

package com.txw.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** *帐户的控制类 * @author Adair */ @Controller @RequestMapping(path = "/account") @SuppressWarnings("all") // 注解警告信息 public class AccountController { @RequestMapping(value = "/findAll") public String findAll(){ System.out.println("表现层:查询所有账户..."); return "list"; } }

5.在WEB-INF目录下创建pages目录,并创建list.xml的代码如下:

<%-- Created by IntelliJ IDEA. User: Adair Date: 2020/7/8 0008 Time: 14:35 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>查询所有的帐户</h3> </body> </html>

6.部署ssm服务器如图所示: 7使用ssm服务器运行如图所示: 8.通过浏览器访问http://localhost:8080/如图所示: 9.点击测试会跳转到如图所示的界面: 10.控制台打印结果如图所示:

最新回复(0)