The front is primarily background code, the integration of spring and MyBatis
The following are mainly SPRINGMVC used to handle request forwarding, Presentation layer processing
Before all done, completed the backstage, the business layer and the development of the persistence layer completed
Next up is the presentation layer.
There are a lot of MVC frameworks, here we use SPRINGMVC
First, you need a jar package.
We're a Web project now.
is also necessary, so the two packages need to be added
We need to configure Web. xml
A Web project, when started, the container here refers to Tomcat this, will first read the Web. XML configuration file inside the configuration
So he's the most fundamental configuration file.
The spring configuration needs to be set here, and the Tomcat container we use will automatically start our spring container.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<!-- 1,从类路径下加载spring的配置文件,classpath关键字特指类路径下加载 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--2,负责启动spring容器的监听器,它将引用1处的上下文参数,获得spring配置文件的地址 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
1. First, we specify the path to the configuration file through the context parameter of the Web. xml
2. Then the Web container listener that specifies the Contextloaderlistener provided by spring
The listener starts automatically when the Web container starts, gets the profile path, reads the configuration file, and starts the spring, based on the value of the parameter at 1.
You also need to configure the servlet to intercept URL requests
<servlet>
<servlet-name>bbs</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>
Attention:
A servlet is configured, and a configuration file is required, which
The default is the Web-inf directory, with the name [<servlet-name>]-servlet.xml
The following is the name and location of the configuration file specified.
To put it bluntly, a servlet is configured to have a control profile, or to write a file according to the location and name of the default specification.
or set a name and location on your own.
It is recommended that you customize a
We're also going to define the interception request, right?
<servlet-mapping>
<servlet-name>bbs</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
This is the name of nature is our servlet name, matching is. Do end, as the truth, you set to anything is possible
So our web. xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<!-- 1,从类路径下加载spring的配置文件,classpath关键字特指类路径下加载 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--2,负责启动spring容器的监听器,它将引用1处的上下文参数,获得spring配置文件的地址 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>bbs</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>bbs</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
Springmvc.xml content is
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!-- 自动扫描带有@Controller注解的控制层,注入为bean -->
<context:component-scan base-package="com.bbs.web" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
After we've configured it, we should start writing the controller.
First, write a test with the most basic
Under our previous web directory, we created a new class, named, Logincontroller
package com.bbs.web;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bbs.service.UserService;
@Controller
public class LoginController {
@Autowired
private UserService userService;
@RequestMapping(value="/login")
public String toLoginPage(){
return "login";
}
}
@controller is the controller's note, huh?
We return a view
The name of the view is login
Remember, we are in the servlet configuration file, this project is springmvc.xml, configuration, page file
Position
Prefix suffix OH
Then we need to create a JSP file, named Login.jsp, according to the location we specify.
<%@ 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">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>标题</title>
<body>
这个是首页
</body>
As above, we've written a line of code as a test.
At this point, our entire process is set up and run
Under Eclipse configuration Tomcat, the project is added (right-click New server to create a server to select the tomcat installed on your computer)
Http://localhost:8080/bbs/login.do
Page configured servlet intercept, go to Controller,controller help us find the page
It's a success.
Http://localhost:8080/bbs/login.do
8080 This is the configuration of the server, look at the quasi-port ha, if it is modified to 80, you do not have to write a can omit
BBS is the name of our project
Login is the configuration in our controller
Do is a suffix in the configuration file
SPRINGMVC Project Complete example of spring MVC