Java +Tomcat + SpringMVC实现页面访问示例解析

网友投稿 280 2022-11-29


Java +Tomcat + SpringMVC实现页面访问示例解析

window7下java环境安装记录:

一、安装Tomcat

1、下载tomcat 7.0,解压,无需安装,放置到目录:D:\apache-tomcat-7.0.90。

2、配置系统环境变量,CATALINA_BASE=D:\apache-tomcat-7.0.90,CATALINA_HOME=D:\apache-tomcat-7.0.90,在Path中新增“%CATALINA_HOME%\lib;%CATALINA_HOME%\bin”的环境变量。

3、进入D:\apache-tomcat-7.0.90\bin,执行startup启动tomcat,浏览器中输入“127.0.0.1:8080”即可查看是否成功。

二、使用SpringMVC搭建页面

1、使用Idea创建SpringMVC工程,webAppTest2

2、修改web/WEB-INF/web.xml的url规则为接受所有

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:dispatcher-servlet.xml

dispatcher

/

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:dispatcher-servlet.xml

dispatcher

/

3、在src下创建dispatcher-servlet.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.1.xsd">

id="internalResourceViewResolver">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.1.xsd">

id="internalResourceViewResolver">

id="internalResourceViewResolver">

4、在src下创建test目录,目录下同时创建MyController文件

package test;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

@RequestMapping("/mvc")

public class MyController {

@RequestMapping("/hello")

public String hello(){

return "hello";

}

}

5、在web/WEB-INF下创建classes和lib文件夹,用来设置编译的.class文件目录和第三方依赖包目录。

6、web/WEB-INF/下创建jsp目录,并在目录下创建hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

Hello world!!!!

7、打war包Build-->Build Artifacts,包名webAppTest2,放到D:\apache-tomcat-7.0.90\webapps目录下。

8、重启tomcat,访问http://127.0.0.1:8080/webAppTest2/mvc/hello 即可。

9、也可以设置Idea内置TomcarsPOCAABt,直接Idea内启动浏览器

10、省略工程名,直接访问即可


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java实现飞机航班管理系统的思路详解
下一篇:浅谈Java实现面向对象编程java oop
相关文章

 发表评论

暂时没有评论,来抢沙发吧~