首页 > 其他分享 >STS用Maven写一个登录页面

STS用Maven写一个登录页面

时间:2022-08-23 20:02:36浏览次数:87  
标签:xml maven plugin Maven jsp STS pages 页面

上一章我们写了一个HelloWorld,这一章在此基础上写出一个登录页面。

 

一、当前目录结构为:

 

 之所以运行后出现HelloWord的页面,是因为server启动时会默认执行index.jsp

 

 

二、改写HelloWorld程序为登录页面

1. 让server启动到我们自己的页面而不是index.jsp

先在webapp下新建一个folder名为pages:右键webapp -》New -》Folder,取名pages。

在pages文件夹下创建一个login.jsp:右键pages -》New -》Other -》Web -》JSP File,取名为login.jsp。

 

 修改web.xml中默认启动文件,这个web.xml就是Demo下的,而不是刚才看到的server下面的。

 然后三步走:

  a. Maven -> Update Project

  b. Run as -> maven install

  c. Run as -> Run onServer

 

 可以看到默认启动的页面已经被修改,那么如果我们想要启动的页面由java的controller去控制,又该如何呢。

2. 创建Java入口文件

在src/main/java下新建一个package名为com.test.demo(这个地方去看pom.xml中定义的groupId),然后此package下新建一个java类名为Demo.java,这个名字可以随便取。

 

 

 

 

编写Java文件

 

 可以看到有些lib都飘红了,到pom.xml里面去把它们配置上。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.test.demo</groupId>
  <artifactId>Demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>Demo Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <org.springframework-version>4.2.6.RELEASE</org.springframework-version>
    <org.slf4j-version>1.7.5</org.slf4j-version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>Demo</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
View Code

 

接着在pages文件夹下新建一个HomeController.jsp

 

 配置web.xml

 

 在WEB-INF下创建一个my-servlet.xml和root-context.xml

my-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.test.demo"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>
View Code

 

root-context.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--  Empty  -->
</beans>
View Code

 

my-servlet指定了controller会访问的位置

 

 这就拼凑成了/pages/HomeController.jsp,所以当value时根目录时,直接访问HomeController.jsp

三步走:

 

如果发现有单词是因为拼写问题飘红,可以【Windows】-》【Preferences】-》【General】-》【Editors】-》【Text Editors】-》【Spelling】,取消Enable spell checking前面的√

 

 

 3. 写一个登录页面

 login.jsp - https://www.cnblogs.com/guo-xu/p/7875760.html

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="login.jsp" method = "get">
        <table align = "center" border = "1" width = "500">
            <tr>
                <td>username: </td>
                <td>
                    <input type = "text" name = "username"/>
                </td>
            </tr>
            <tr>
                <td>password:</td>
                    <td>
                        <input type = "password" name = "password"/>
                    </td>
            </tr>
            <tr align = "center">
                    <td colspan = "2">
                            <input type = "submit" value = "login"/>
                            <input type = "reset" value = "reset"/>
                    </td>
            </tr>
        </table>
    </form>
</body>
</html>
View Code

 

Demo.java

 

 运行程序:

 

 成功写了一个登录界面,后续在写如何如何动态登录。

 

标签:xml,maven,plugin,Maven,jsp,STS,pages,页面
From: https://www.cnblogs.com/smart-zihan/p/16617586.html

相关文章