码迷,mamicode.com
首页 > 编程语言 > 详细

Intellij IDEA +MAVEN+Jetty实现SpringMVC简单查询功能

时间:2016-12-23 13:35:22      阅读:1000      评论:0      收藏:0      [点我收藏+]

标签:技术分享   inf   standard   tty   repos   ica   tag   success   javaee   

1 pom.xml

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3   <modelVersion>4.0.0</modelVersion>
  4   <groupId>TestSpringMVC2</groupId>
  5   <artifactId>TestSpringMVC2</artifactId>
  6   <packaging>war</packaging>
  7   <version>1.0-SNAPSHOT</version>
  8   <name>TestSpringMVC2 Maven Webapp</name>
  9   <url>http://maven.apache.org</url>
 10 
 11   <dependencies>
 12     <dependency>
 13       <groupId>junit</groupId>
 14       <artifactId>junit</artifactId>
 15       <version>3.8.1</version>
 16       <scope>test</scope>
 17     </dependency>
 18 
 19       <dependency>
 20         <groupId>org.springframework</groupId>
 21         <artifactId>spring-jdbc</artifactId>
 22         <version>3.0.5.RELEASE</version>
 23       </dependency>
 24 
 25     <dependency>
 26       <groupId>org.springframework</groupId>
 27       <artifactId>spring-test</artifactId>
 28       <version>4.3.1.RELEASE</version>
 29     </dependency>
 30     <!--spring-->
 31     <dependency>
 32       <groupId>org.springframework</groupId>
 33       <artifactId>spring-beans</artifactId>
 34       <version>4.3.1.RELEASE</version>
 35     </dependency>
 36 
 37     <dependency>
 38       <groupId>org.springframework</groupId>
 39       <artifactId>spring-core</artifactId>
 40       <version>4.3.1.RELEASE</version>
 41     </dependency>
 42 
 43     <dependency>
 44       <groupId>org.springframework</groupId>
 45       <artifactId>spring-context</artifactId>
 46       <version>4.3.1.RELEASE</version>
 47     </dependency>
 48 
 49     <!--Spring Web + Spring MVC-->
 50     <dependency>
 51       <groupId>org.springframework</groupId>
 52       <artifactId>spring-web</artifactId>
 53       <version>4.3.1.RELEASE</version>
 54     </dependency>
 55     <dependency>
 56       <groupId>org.springframework</groupId>
 57       <artifactId>spring-webmvc</artifactId>
 58       <version>4.3.1.RELEASE</version>
 59     </dependency>
 60 
 61     <!--NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config-->
 62     <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
 63     <dependency>
 64       <groupId>javax.servlet</groupId>
 65       <artifactId>jstl</artifactId>
 66       <version>1.2</version>
 67     </dependency>
 68 
 69     <dependency>
 70       <groupId>taglibs</groupId>
 71       <artifactId>standard</artifactId>
 72       <version>1.1.2</version>
 73     </dependency>
 74 
 75 
 76     <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
 77     <dependency>
 78       <groupId>javax.servlet.jsp</groupId>
 79       <artifactId>jsp-api</artifactId>
 80       <version>2.2</version>
 81     </dependency>
 82 
 83 
 84   </dependencies>
 85 
 86   <build>
 87     <finalName>TestSpringMVC2</finalName>
 88 
 89     <resources>
 90       <!--表示把java目录下的有关xml文件,properties文件编译/打包的时候放在resource目录下-->
 91       <resource>
 92         <directory>${basedir}/src/main/java</directory>
 93         <includes>
 94           <include>**/*.properties</include>
 95           <include>**/*.xml</include>
 96         </includes>
 97       </resource>
 98       <resource>
 99         <directory>${basedir}/src/main/resources</directory>
100       </resource>
101     </resources>
102     <plugins>
103       <!--servlet容器 jetty插件-->
104       <plugin>
105         <groupId>org.eclipse.jetty</groupId>
106         <artifactId>jetty-maven-plugin</artifactId>
107         <version>9.3.10.v20160621</version>
108       </plugin>
109     </plugins>
110   </build>
111 </project>

2 web.xml

 1 <!DOCTYPE web-app PUBLIC
 2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4   <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 5            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 7           http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 8            version="3.0">
 9 
10     <!--配置springmvc DispatcherServlet-->
11     <servlet>
12           <servlet-name>springMVC-servlet</servlet-name>
13           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
14           <init-param>
15                <param-name>contextConfigLocation</param-name>
16               <param-value>classpath:springMVC-servlet.xml</param-value>
17            </init-param>
18 
19          <load-on-startup>1</load-on-startup>
20     </servlet>
21 
22     <servlet-mapping>
23         <servlet-name>springMVC-servlet</servlet-name>
24         <url-pattern>/</url-pattern>
25     </servlet-mapping>
26 
27     <!-- 配置HiddenHttpMethodFilter:把POST 请求转为DELETE/PUT 请求-->
28     <filter>
29         <filter-name>HiddenHttpMethodFilter</filter-name>
30         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
31     </filter>
32     <filter-mapping>
33         <filter-name>HiddenHttpMethodFilter</filter-name>
34         <url-pattern>/*</url-pattern>
35     </filter-mapping>
36 
37     <context-param>
38       <param-name>contextConfigLocation</param-name>
39       <param-value>classpath:applicationContext.xml</param-value>
40     </context-param>
41 
42       <listener>
43         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
44       </listener>
45 
46 </web-app>

3 applicationContext.xml

    

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans
3         xmlns="http://www.springframework.org/schema/beans"
4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5         xmlns:p="http://www.springframework.org/schema/p"
6         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
7     <!-- 我们可以在其中添加我们所需要配置的bean,也可以添加相应的数据库连接和事务处理等等,方便后续拓展
8            -->
9 </beans>

4 springMVC-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:mvc="http://www.springframework.org/schema/mvc"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 8 http://www.springframework.org/schema/context
 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd
10 http://www.springframework.org/schema/mvc
11 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
12     <mvc:annotation-driven />
13     <mvc:default-servlet-handler/>
14 
15     <!-- 启动包扫描功能 -->
16     <context:component-scan base-package="com.springmvc.crud" />
17 
18     <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
19     <bean id="viewResolverCommon" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
20         <property name="prefix" value="/WEB-INF/views/"/>
21         <property name="suffix" value=".jsp"/>
22         <property name="viewClass"  value="org.springframework.web.servlet.view.JstlView" />
23     </bean>
24 
25 </beans>

5 Employee.java

 1 package com.springmvc.crud.entities;
 2 
 3 /**
 4  * Created by wanggenshen_sx on 2016/12/23.
 5  */
 6 public class Employee {
 7     private Integer id;
 8     private String lastName;
 9     private String email;
10 
11 
12     public Integer getId() {
13         return id;
14     }
15 
16     public void setId(Integer id) {
17         this.id = id;
18     }
19 
20     public String getLastName() {
21         return lastName;
22     }
23 
24     public void setLastName(String lastName) {
25         this.lastName = lastName;
26     }
27 
28     public String getEmail() {
29         return email;
30     }
31 
32     public void setEmail(String email) {
33         this.email = email;
34     }
35 
36     @Override
37     public String toString() {
38         return "Employee [id=" + id + ", lastName=" + lastName + ", email="
39                 + email +  "]";
40     }
41 
42     public Employee(Integer id, String lastName, String email) {
43         super();
44         this.id = id;
45         this.lastName = lastName;
46         this.email = email;
47     }
48 
49     public Employee() {
50     }
51 }

6 EmployeeDao.java

 1 package com.springmvc.crud.dao;
 2 
 3 import com.springmvc.crud.entities.Employee;
 4 import org.springframework.stereotype.Repository;
 5 
 6 import java.util.Collection;
 7 import java.util.HashMap;
 8 import java.util.Map;
 9 
10 /**
11  * Created by wanggenshen_sx on 2016/12/23.
12  */
13 @Repository
14 public class EmployeeDao {
15 
16     private static Map<Integer,Employee> employees=null;
17 
18     static{
19         employees = new HashMap<Integer, Employee>();
20 
21         employees.put(1001, new Employee(1001, "E-AA", "aa@163.com"));
22         employees.put(1002, new Employee(1002, "E-BB", "bb@163.com"));
23         employees.put(1003, new Employee(1003, "E-CC", "cc@163.com"));
24         employees.put(1004, new Employee(1004, "E-DD", "dd@163.com"));
25         employees.put(1005, new Employee(1005, "E-EE", "ee@163.com"));
26     }
27 
28     private static Integer initId=1006;
29 
30     public void save(Employee employee){
31         if(employee.getId()==null){
32             employee.setId(initId++);
33         }
34         employees.put(employee.getId(), employee);
35     }
36 
37     public Collection<Employee> getAll(){
38         return employees.values();
39     }
40     public Employee get(Integer id){
41         return employees.get(id);
42     }
43 
44 }

7 EmployeeController.java

 1 package com.springmvc.crud.controller;
 2 
 3 import com.springmvc.crud.dao.EmployeeDao;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.stereotype.Repository;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestMethod;
 9 
10 import java.util.Map;
11 
12 /**
13  * Created by wanggenshen_sx on 2016/12/23.
14  */
15 @Controller
16 public class EmployeeController {
17     @Autowired
18     private EmployeeDao employeeDao;
19 
20     @RequestMapping(value ="listAll",method = RequestMethod.GET)
21     public  String list(Map<String,Object> map){
22         map.put("employees",employeeDao.getAll());
23         return "list";
24     }
25 }

8 success.jsp

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: wanggenshen_sx
 4   Date: 2016/12/23
 5   Time: 9:10
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <title>success</title>
12 </head>
13 <body>
14         Hello SpringMVC!
15 </body>
16 </html>

技术分享

 

 

Intellij IDEA +MAVEN+Jetty实现SpringMVC简单查询功能

标签:技术分享   inf   standard   tty   repos   ica   tag   success   javaee   

原文地址:http://www.cnblogs.com/noaman/p/6214268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!