1. 程式人生 > >從0開始整合SSM框架--3.整合SpringMvc

從0開始整合SSM框架--3.整合SpringMvc

前面面已經完成了2大框架的整合,SpringMVC的配置檔案單獨放,然後在web.xml中配置整合。
1.配置spring-mvc.xml


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


<!-- 掃描帶Controller註解的類 -->
<context:component-scan base-package="com.fanwei.myssm.controller" />
<!-- 載入註解驅動 -->
<mvc:annotation-driven/>
<!-- 配置檢視直譯器 jsp -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>


2.配置web.xml檔案
<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">


<!-- 上下文的位置 -->
<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>

<!-- POST提交過濾器 UTF-8 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<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:Spring/spring-mvc.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>


<!-- POST提交過濾器 UTF-8 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<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:Spring/spring-mvc.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>

4.編寫service
目錄結構如下:

 

程式碼如下:
UserService
public interface UserService {

public User findById(Integer id) throws Exception;
}


UserServiceImpl
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;

@Override
public User findById(Integer id) throws Exception{
return userDao.findUserById(id);
}
}

 

注意:不要忘記在applicationContext.xml 新增掃描包,載入service

 

<!--掃描service-->
<context:component-scan base-package="com.fanwei.myssm.service"/>

 

 

5. 編寫Controller
@Controller
public class ssmController {
@Resource
private UserService userService;

@RequestMapping(value="/login")
public String HelloWorld(Model model){
return "login";
}

@RequestMapping(value = "/user/login")
public String login(Integer id,Model model){
User user = null;
try{
user = userService.findById(id);
}catch (Exception e){
e.printStackTrace();
}
model.addAttribute("username",user.getUsername());
model.addAttribute("sex",user.getSex());
return "success";
}
}

login.jsp中程式碼
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<body>
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Horizontal Form</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form class="form-horizontal" action="/user/login" method="post" id="registerForm">
<div class="box-body">
使用者Id <input id="id" name="id">
</div>
<!-- /.box-body -->
<div class="box-footer">
<button id = "submit" type="submit">根據id查詢使用者資訊</button>
</div>
<!-- /.box-footer -->
</form>
</div>
</body>
</html>

success.jsp中程式碼
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<h1>使用者名稱:${username}</h1>
<h1>性別:${sex}</h1>
<title>查詢成功</title>
</head>
<body>

</body>
</html>

 

注意:
在引入Springmvc以後載入檔案時需要在檔案全限定名前面加上classpath:否則會報

HTTP Status 500 - Servlet.init() for servlet springmvc threw exception

 

 

整個專案結構圖