欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SSM 框架 Maven项目 项目整合 SpringMVC

程序员文章站 2022-05-04 12:36:56
...
原文地址为:SSM 框架 Maven项目 项目整合 SpringMVC

项目结构

 SSM 框架 Maven项目 项目整合 SpringMVC

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>

<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<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监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!--Spring MVC 配置servlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

springmvc.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<!-- 自动扫描该包 @controller注解的类-->
<context:component-scan base-package="com.zyh.controller"/>

<!-- 静态资源处理 -->
<mvc:default-servlet-handler/>

<!--自动注册 RequestMappingHandlerMapping、RequestMappingHandlerAdapter 等bean -->
<mvc:annotation-driven></mvc:annotation-driven>

<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
UserController.java

package com.zyh.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zyh.entity.Userinfo;
import com.zyh.service.UserService;

@Controller
public class UserController {

private static Logger logger = Logger.getLogger(UserController.class);

@Autowired
UserService userService;

@RequestMapping("/showUser")
public String testtoshowUser(@RequestParam(value = "id") String id, Model model) {
logger.info("测试类");
Userinfo userinfo = userService.getUserById(id);
model.addAttribute("user", userinfo);
return "show";
}
}

index.jsp

<html>
<body>
<h2>Hello World!</h2>

<a href="showUser?id=1">ShowUser page</a><br>

</body>
</html>
show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>


id:${user.id} <br>
userName: ${user.name} <br>
password: ${user.password} <br>
phone: ${user.phone} <br>

</body>
</html>
添加Tomcat 运行即可。

效果

SSM 框架 Maven项目 项目整合 SpringMVC
SSM 框架 Maven项目 项目整合 SpringMVC

项目下载地址 :http://download.csdn.net/download/z_demon801/10217874


转载请注明本文地址:SSM 框架 Maven项目 项目整合 SpringMVC