IDEA creates maven project + spring MVC framework to complete the same functions as servlet

1, Create maven project First create the maven project. Then set the project name and configuration address. Final...
2. servlet operation

1, Create maven project
First create the maven project.

Then set the project name and configuration address.

Finally, the project is created successfully. The file is shown below.


2, Add dependency
Find the tag in pom.xml and add the dependency to it.

The code is as follows.

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--journal--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.10</version> </dependency> <!--J2EE--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--mysql Driver package--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <!--springframework--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>com.github.stefanbirkner</groupId> <artifactId>system-rules</artifactId> <version>1.16.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <!--Other packages required--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>


3, Build spring MVC framework
1. Add spring
Right click the item and select the option in the figure.

Drop down to find spring and select spring and Spring MVC.

Because the previous spring configuration was not downloaded, you can download it.

Two xml files appear after success.


2. Add folder
First create the java and resources folders under src/main folder, and then create the test folder under src. After creation, mark the java, resources and test folders. Right click the folder, select Mark Directory as, and select the folder type you want.

Create four packages under the java folder, named as follows.

2. Spring MVC settings
(1) Configure 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 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <!--welcome pages--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--to configure springmvc DispatcherServlet--> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!--to configure dispatcher.xml As mvc Configuration file for--> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--hold applicationContext.xml Add to profile--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>


(2) Configure dispatcher-servlet.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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--This document is responsible for the entire mvc Configuration in--> <!--Enable spring Some of annotation --> <context:annotation-config/> <!-- The annotation driver can be configured to request Parameters and bound to controller On parameters --> <mvc:annotation-driven/> <!--Static resource mapping--> <!--This project puts static resources in webapp of statics Under the directory, the resource mapping is as follows--> <mvc:resources mapping="/css/**" location="/statics/css/"/> <mvc:resources mapping="/js/**" location="/statics/js/"/> <mvc:resources mapping="/image/**" location="/statics/images/"/> <mvc:default-servlet-handler /> <!--This sentence should be added, otherwise you may not be able to access static resources, and the specific role is Baidu itself--> <!-- The resolution of the model view name, that is, the suffix is added before the model view name(If the last one still represents a folder,Don't miss the last slash) use JSP--> <!-- The default view parser is used when the above parsing error occurs (Default use html)- --> <bean id="defaultViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/views/"/><!--set up JSP Directory location of the file--> <property name="suffix" value=".jsp"/> <property name="exposeContextBeansAsAttributes" value="true"/> </bean> <!-- Automatic scanning assembly --> <context:component-scan base-package="example.controller"/> </beans>


(2) Configure applicationContext.xml

Finally, configure applicationContext.xml to be responsible for the configuration of some non MVC components. The code is as follows.

<?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" 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.xsd"> <context:component-scan base-package="example"/> </beans>



4, Test
1. Testing
Create a DemoController.java in the controller package. The code is as follows.

package example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/home") public class DemoController { @RequestMapping("/index") public String index(){ return "index"; } }


Create a new index.jsp in the views folder,

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Index</title> <link rel="stylesheet" type="text/css" href="../statics/css/index.css"> </head> <body> <h2>Test success!</h2> </body> </html>

Create a new index.css under statistics / CSS.

h2{ color: darkred; }

Then configure Tomcat, click Run, and select Edit Configurations.

  Click the plus sign to find Tomcat server -- > local

  First configure Deployment, click the plus sign and select Artifact.

  After clicking successfully, the following figure appears.

  Then set up the Server. You can give Tomcat a name. Configure selects the Tomcat you installed and sets a default browser according to your personal habits. Then select Update classes and resources for On 'Update' action and On frame deactivation. This is to take effect immediately when we modify static resources such as jsp, css and js. We can see the effect directly On the page without restarting Tomcat.

  After the configuration is successful, run Tomcat and enter in the address bar http://localhost:8080/springMVC_test/home/index, and it's OK.

2. servlet operation

Introduce related dependencies in pom.xml

<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies>

Build a module servlet under the root directory. To use a servlet, you need the support of the web framework.
After the creation is successful, add the following code.

<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> </dependencies>

Then write a Servlet class to process user requests.

package com.kuang.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1. Get front-end parameters String method = req.getParameter("method"); if(method.equals("add")){ req.getSession().setAttribute("msg","Yes add method"); } if(method.equals("delete")){ req.getSession().setAttribute("msg", "Yes delete method"); } // 2. Call the business layer // 3. View forwarding or redirection req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }

Write a jsp file to display the form for submission.

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/hello" method="post"> <input type="text" name="method"> <input type="submit"> </form> </body> </html>

Finally, write a test jsp and get feedback after the form is submitted.

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> $ </body> </html>

3 October 2021, 16:41 | Views: 8111

Add new comment

For adding a comment, please log in
or create account

0 comments