当前位置: 移动技术网 > IT编程>开发语言>Java > springmvc+maven搭建web项目

springmvc+maven搭建web项目

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了springmvc maven搭建web项目的具体步骤,供大家参考,具体内容如下

1、创建一个maven project 为spring1

2、进行项目的配置:默认的java 1.5 在properties中选择project facts项目进行配置,反选web之后修改java环境为1.8.修改之后如下图:

3. 配置好的工作目录如下:

4 修改pom.xml文件

增加两个jar包:servlet和spring webmvc

pom.xml如下:

<!doctype web-app public
"-//sun microsystems, inc.//dtd web application 2.3//en"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

4. 默认的配置文件是spring-servlet.xml ,在/web-inf下创建配置文件,内容如下:

<?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:tx="http://www.springframework.org/schema/tx" 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/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- 配置扫描的包 -->
<context:component-scan base-package="spring1.*" />

<!-- 注册handlermapper、handleradapter两个映射类 -->
<mvc:annotation-driven />

<!-- 访问静态资源 -->
<mvc:default-servlet-handler />

<!-- 视图解析器 -->
<bean
>
<property name="prefix" value="/web-inf/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

注:在这个默认的配置过程中 程序会扫描spring1包中的所有的controller

5、 在src/main/java创建controller

package spring1;

import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;

@controller
public class democontroller {

@requestmapping("/index")
public string index(){
return "index";
}
}

6、 在webapp目录下创建index.jsp.

<html>
<body>
<h2>hello world!</h2>
</body>
</html>

7、 部署项目到tomcat上,部署的内容如下:

8、启动tomcat 在浏览器中输入:http://www.lhsxpumps.com/_localhost:8080/spring1/

总结:调试了好久,终于调试出来信息啦 小小激动一下啊

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网