In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article "how to use IDEA to integrate the SSM framework and achieve the display of data on the web page" most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "how to use IDEA to integrate the SSM framework and achieve data display on the web page" article bar.
The first step,
Create a maven project. Configure Tomcat and test whether you have normal access to HelloWorld.
This step is omitted.
The second step,
In pom.xml import dependencies, the following dependencies are commonly used in ssm, and there is no harm in importing them.
UTF-8 1.7 1.7 2.9.0 junit junit 4.11 test org.springframework spring-webmvc 5.1.3.RELEASE aopalliance aopalliance 1.0 org.aspectj aspectjweaver 1.9.2 org.apache.commons commons-dbcp2 2.7.0 Org.apache.commons commons-pool2 2.8.0 org.springframework spring-aspects 5.1.3.RELEASE org.springframework spring-test 5.1.3.RELEASE mysql mysql-connector-java 5.1.46 com.mchange c3p0 0.9.5.2 Org.mybatis mybatis 3.4.5 org.springframework spring-jdbc 5.1.3.RELEASE org.mybatis mybatis-spring 1.3.1 org.springframework spring-tx 5.1.3.RELEASE org.projectlombok lombok 1.18.10 net.sf.json-lib Json-lib 2.4 jdk15 commons-beanutils commons-beanutils 1.7.0 commons-collections commons-collections 3.1 commons-lang commons-lang 2.5 net.sf.ezmorph ezmorph 1.0.3 commons-logging commons-logging 1.2 com. Alibaba druid 1.2.6 javax.servlet.jsp.jstl jstl-api 1.2-rev-1 taglibs standard 1.1.2 org.apache.taglibs taglibs-standard-spec 1.2.5 org.apache.taglibs taglibs-standard-impl 1.2.5 com.fasterxml.jackson.core Jackson-databind ${jackson.version} com.fasterxml.jackson.core jackson-core ${jackson.version} com.fasterxml.jackson.core jackson-annotations ${jackson.version} com.alibaba easyexcel 2.1.1 org.apache.poi poi 3.17 org.apache.poi poi-ooxml Step 3. 17 src/main/java * * / * .xml,
Create a data table.
Create a data table named category_ with only two fields, one id and one name,id increment. Feel free to insert point data.
DROP TABLE IF EXISTS `category_ `; CREATE TABLE `category_` (`id` int (11) NOT NULL AUTO_INCREMENT, `name` varchar (255) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;-- Records-INSERT INTO `category_ `VALUES ('1th,' sadf') INSERT INTO `category_ `VALUES ('2Chou,' safa'); INSERT INTO `adfasdfas' `VALUES ('3INSERT INTO,' adfasdfas'); INSERT INTO `category_ `VALUES ('415,' Adolf Zhang'); INSERT INTO `category_ `VALUES ('515,' illegal charge'); INSERT INTO `category_ `VALUES ('615,' v'); INSERT INTO `category_ `VALUES ('74th,' sdfsd'); INSERT INTO `category_ `VALUES ('84th,' 34535')
Step 4,
Write entity classes; DAO layer; Service layer; Controller layer.
Like me, create these packages in the java folder directory.
Then create the corresponding java file or configuration file under the package, and the final project structure is as follows:
You don't need those mosaics, kids, don't look at them. )
Next, start writing entity classes:
It is best to see the name of the entity class and match the name of the data table. Because the lombok plug-in is used, the @ Data annotation is used to reduce the amount of code.
Category.java
Package com.entity;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@AllArgsConstructor@NoArgsConstructorpublic class Category {private int id; private String name;}
Then there is the DAO layer, where most of what is written here is the interface, only the method, not the implementation.
CategoryDao.java
Package com.dao;import com.entity.Category;import java.util.List;public interface CategoryDao {public int add (Category category); public void delete (int id); public Category get (int id); public int update (Category category); public List list ();}
Then create the corresponding Mapper.xml file under the Dao layer.
Note that the namespace matches, and the id corresponds to the method name.
CategoryMapper.xml
Insert into category_ (name) values (# {name}) delete from category_ where id=# {id} select * from category_ where id=# {id} update category_ set name=# {name} where id=# {id} select * from category_
Then there is the Service layer, which is where you want to implement your business. Here is also the interface, in the impl implementation class is to write the real implementation.
There is only one implementation here: query all data.
CategoryService.java
Package com.service;import com.entity.Category;import java.util.List;public interface CategoryService {List list ();}
Then write the implementation class.
CategoryServiceImpl.java
Package com.service.impl;import com.dao.CategoryDao;import com.entity.Category;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.service.CategoryService;import java.util.List;@Servicepublic class CategoryServiceImpl implements CategoryService {@ Autowired CategoryDao categoryDao; @ Override public List list () {return categoryDao.list ();}
Then write the Controller layer.
IndexController.java
Package com.controller;import com.entity.Category;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.servlet.ModelAndView;import com.service.CategoryService;import java.util.ArrayList;import java.util.List;@Controller@RequestMapping ("/") public class IndexController {@ Autowired CategoryService categoryService @ RequestMapping ("listCategory") public ModelAndView listCategory () {ModelAndView mav = new ModelAndView (); List cs= categoryService.list (); / / put the forwarding parameter mav.addObject ("cs", cs); / / put the jsp path mav.setViewName ("listCategory"); return mav;}}
OK, at this point, you are already half done! Applaud yourself!
Next, let's cheer up, write the configuration file and access the data!
Step 5,
Write a configuration file.
In the resources directory, create two configuration files.
Pay attention to change the database, password, to your own.
ApplicationContext.xml
Com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8 root root
SpringMVC.xml
Finally, there is web.xml.
Web.xml
ContextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener mvc-dispatcher org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springMVC.xml 1 mvc-dispatcher / step 6,
Create a listCategory.jsp file.
Create the jsp folder under the WEB-INF directory and put all the jsp files here, because what we configured in springMVC.xml won't explain much.
I don't know where I created it. I can look back at my project path and follow that one.
ListCategory.jsp
Title id name ${c.id} ${c.name}
I won't say much about the idea of getting the data here. I know it all. It is the jstl tag in the jsp page to get the data returned in the Controller.
OK, come here, it's done! Start Tomcat and test it.
No problem, note that the access path here, http://localhost:8885/listCategory, is not to access the jsp page directly, but to access the request path in controller.
Ok, that's it.
A small improvement here is the change in controller, which uses the @ ResponseBody annotation to return data, and then the front end requests data asynchronously through ajax.
ListController.java
Package com.controller;import com.entity.Category;import com.service.CategoryService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import java.util.List;@Controller@RequestMapping ("/") public class ListController {@ Autowired CategoryService categoryService @ RequestMapping ("list/allCategory") @ ResponseBody public List findAll () {List list = categoryService.list (); System.out.println (list.size ()); return list;} @ RequestMapping ("/ listall") public String listAll () {return "listall";}}
Create a listall.jsp page under the jsp folder.
Listall.jsp
Home id name $(function () {$('# btn') .click (function () {$.post ("list/allCategory", function (data) {console.log (data); var html = ""; for (var I = 0; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.