首先,在Eclipse中导入Maven工程后,我们需要对项目进行如下配置:
- 打开pom.xml文件,添加如下依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
其中,Spring Web是一个MVC框架,提供了针对HTTP请求和响应的处理类。
- 修改pom.xml文件,添加Tomcat7的插件依赖:
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<path>/</path>
<port>8080</port>
<uriEncoding>UTF-8</uriEncoding>
<contextReloadable>true</contextReloadable>
<useFileMappedBuffer>false</useFileMappedBuffer>
</configuration>
</plugin>
</plugins>
</build>
- 将项目移动到Tomcat7的webapps目录下:
右键单击项目,选择Export -> WAR file,选择Tomcat7/webapps目录,点击Finish。
- 启动Tomcat服务器:
在Eclipse中,右键单击Tomcat服务器,选择Start。
- 访问项目:
在浏览器中输入http://localhost:8080/{项目名},即可访问。
示例1:使用eclipse、maven、spring3.x、jsp等技术实现简单的注册登录功能,具体步骤如下:
-
创建Maven工程
-
修改pom.xml文件,添加Spring和JSP等依赖:
<dependencies>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!--Servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!--JSP-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet.jsp</artifactId>
<version>2.3.2-b06</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>1.2.1</version>
</dependency>
<!--数据库-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
-
创建Java类并编写相应的逻辑代码,如:UserController类、UserDao类、User实体类。
-
创建JSP页面,如:register.jsp、login.jsp
-
配置Web.xml文件,配置Spring MVC的DispatcherServlet:
<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-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>
- 配置Spring MVC文件spring-mvc.xml:
<beans>
<context:component-scan base-package="com.example" />
<mvc:annotation-driven />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db_user" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
示例2:使用eclipse、maven、spring boot等技术实现简单的Web应用,具体步骤如下:
-
创建Maven工程
-
修改pom.xml文件,添加Spring Boot和JSP等依赖:
<dependencies>
<!--Spring Boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--JSP-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--FastJson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
<!--数据库-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
-
创建Java类并编写相应的逻辑代码,如:UserController类、User实体类。
-
创建JSP页面,如:register.jsp、login.jsp
-
配置application.properties文件,配置数据库信息:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db_user
spring.datasource.username=root
spring.datasource.password=root
- 启动Spring Boot应用,访问Web页面:
@SpringBootApplication
@Controller
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@GetMapping("/")
public String index() {
return "index";
}
@GetMapping("/register")
public String register() {
return "register";
}
@GetMapping("/login")
public String login() {
return "login";
}
//省略业务代码
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解eclipse中Maven工程使用Tomcat7以上插件的方法 - Python技术站