标签:apache 密码 使用 unit hot href name post bsp
我们在上一篇《Maven项目集成Jetty》项目的基础上,集成Spring MVC。
1、配置springmvc pom依赖。
<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>com.coshaho</groupId> <artifactId>spring-mvc-demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>spring-mvc-demo 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> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.4.RELEASE</version> </dependency> </dependencies> <build> <finalName>spring-mvc-demo</finalName> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.15.v20190215</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <httpConnector> <port>8080</port> </httpConnector> <webApp> <contextPath>/coshaho</contextPath> </webApp> </configuration> </plugin> </plugins> </build> </project>
2、配置Spring文件。
<?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:p="http://www.springframework.org/schema/p" 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-3.0.xsd"> <context:component-scan base-package="com.coshaho"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp"></bean> </beans>
spring文件包括包路径扫描,注入InternalResourceViewResolver类。这里注意p:prefix与p:suffix,比如在控制器中我们return "success",则表示跳转到../success.jsp页面。
3、配置web.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> <display-name>Spring MVC Demo</display-name> <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.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
这里关键是配置DispatcherServlet。其中contextConfigLocation值为spring配置文件的路径。
4、Controller类。
package com.coshaho; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class LoginAction { @RequestMapping("login.do") public String login(String username,String password) { if ("admin".equals(username)) { System.out.println(username +" 登录成功"); return "success"; } return "error"; } }
5、index.jsp页面。
<%@ page contentType="text/html;charset=GBK"%> <html> <body> <form action="login.do" method="post"> 用户名:<input type="text" name = "username" ><p> 密码:<input type="password" name = "password" ><p> <input type="submit" value="登录"> </form> </body> </html>
index.jsp页面点击提交跳转到LoginAction中。success.jsp与error.jsp只包含success与error两个字符串。
6、使用mvn jetty:run运行工程,结果如下。
7、工程目录如下。
标签:apache 密码 使用 unit hot href name post bsp
原文地址:https://www.cnblogs.com/coshaho/p/10513635.html