标签:后缀 prefix turn xml文件 inf 文件中 tomcat tle apach
一、新建maven项目,输入组织机构及项目名称
二、在pom.xml文件中引入所需jar包依赖
刚建好的工程会报错 :找不到HttpServlet类 同样在pom.xml文件中引入即可
pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.zf</groupId> <artifactId>springmvc</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springmvc Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- HttpServlet所需jar包--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!--springmvc所需jar包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.2.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.2.RELEASE</version> <scope>compile</scope> </dependency> </dependencies> <build> <finalName>springmvc</finalName> </build> </project>
三、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 直接进工程的spring文件下找springmvc.xml文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
四、建立controller类
@Controller @RequestMapping("/index") public class DemoController { @RequestMapping("/text") public String index(){ System.err.println(123); return "index";//会根据springmvc配置的路径找到以index.jsp文件(.jsp也是在springmvc.xml中配置) } }
五、配置springmvc.xml文件
<?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="cn.zf.*" /> <!-- 注册HandlerMapper、HandlerAdapter两个映射类 --> <mvc:annotation-driven /> <!-- 访问静态资源 --> <mvc:default-servlet-handler /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <!--页面都是放在WEB-INF的view文件夹下--> <property name="suffix" value=".jsp"></property> <!--以.jsp后缀--> </bean> </beans>
六、测试jsp文件
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>首页</title> </head> <body> <form action="index/text" method="post"> 用户名:<input type="text" name="userName"><br/> 密 码:<input type="password" name="password"/><br/> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </form> </body> </html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>首页</title> </head> <body> 登录成功 </body> </html>
七、启动tomcat并测试
启动
输入 clean tomcat:run
访问工程
目录结构
标签:后缀 prefix turn xml文件 inf 文件中 tomcat tle apach
原文地址:https://www.cnblogs.com/xiaotong1223/p/9373733.html