博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用Maven创建第一个web项目Struts2项目
阅读量:4677 次
发布时间:2019-06-09

本文共 3727 字,大约阅读时间需要 12 分钟。

一.创建一个web项目

参考前面文章,项目名:maven-struts-demo。

二.配置pom.xml文件添加struts2依赖

<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.lei.demo</groupId>

  <artifactId>maven-struts-demo</artifactId>

  <packaging>war</packaging>

  <version>0.0.1-SNAPSHOT</version>

  <name>maven-struts-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>

    <dependency>

        <groupId>org.apache.struts</groupId>

        <artifactId>struts2-core</artifactId>

        <version>2.3.8</version>

</dependency>

  </dependencies>

  <build>

    <finalName>maven-struts-demo</finalName>

  </build>

</project>

保存后maven会自动下载相应的jar包。

下载完成后查看jar包,如图

https://images0.cnblogs.com/blog/201693/201310/17163559-7861b03cf76f4b4486bebb011c8bfcbd.png

 

三.新建JSP页面

1.index.jsp页面,点“去登录界面”后,去struts.xml中找对应的anction中name=user_login_go的路径

<%@ page 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>

<body>

    <h2>Struts2-Demo</h2>

    <a href="user_login_go">去登录界面</a>

</body>

</html>

 

2.login.jsp页面,输入name和password后,去struts.xml中找对应的anction中name=login_go的路径

<%@ page 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>struts2-Demo-登录界面</title>

</head>

<body>

    <p>struts2-Demo-登录界面</p>

    <form action="login_go" method="post">

        name:<input type="text" name="name" />

        password<input type="password" name="password" />

        <input type="submit" value="登录" />

    </form>

</body>

</html>

 

3.welcome.jsp页面,输入name和password后,去struts.xml中找对应的anction中name=login_go的路径

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

<%@ taglib prefix="s" uri="/struts-tags" %>

<!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>Struts2-Demo-欢迎页面</title>

</head>

<body>

    Welcome:

    <br>

    <h1>name=<s:property value="name" /></h1>

    <h1>password=<s:property value="password" /></h1>   

    <h1>重新登录</h1>

    <s:form action="login_go" namespace="/" method="post">

        <s:textfield name="name" label="name"></s:textfield> 

        <s:password name="password" label="password"></s:password> 

             

        <s:submit value="Login"></s:submit>

    </s:form>

</body>

</html>

 

4.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>Struts2 Web Application</display-name>

    <filter>

        <filter-name>struts2</filter-name>

        <filter-class>

            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

        </filter-class>

    </filter>

    <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

</web-app>

 

5.struts.xml,放在src/main/resources目录下

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

<!DOCTYPE struts PUBLIC 

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 

    "http://struts.apache.org/dtds/struts-2.0.dtd"> 

<struts>

    <package name="user" namespace="/"  extends="struts-default">

        <action name="user_login_go" class="com.sulei.action.UserLoginAction" method="user_login">

            <result name="success">/login.jsp</result>

        </action>

        <action name="login_go" class="com.sulei.action.UserLoginAction" method="login">

            <result name="success">/welcome.jsp</result>

        </action>

    </package>

</struts>

 

目录结构:

https://images0.cnblogs.com/blog/201693/201310/18150805-5a4b0d86ac62491099b65004eaea1685.png

运行效果如下

https://images0.cnblogs.com/blog/201693/201310/18145920-f94b128e77184c0a9ee144246ff87b7b.png

 

https://images0.cnblogs.com/blog/201693/201310/18145928-297fd7135e9e4b929676e159d0af267d.png

 

 

https://images0.cnblogs.com/blog/201693/201310/18145928-297fd7135e9e4b929676e159d0af267d.png

 

https://images0.cnblogs.com/blog/201693/201310/18145938-9439884d37b6445c97e2e7f7f27328ba.png

 

转载于:https://www.cnblogs.com/yachao1120/p/10467766.html

你可能感兴趣的文章
JavaScript知识(二)
查看>>
Windows phone 8 学习笔记(7) 设备
查看>>
SQL Server的备份
查看>>
SQL Server 重置Identity标识列的值(INT爆了)
查看>>
需求获取常见的方法是进行客户访谈,结合你的实践谈谈会遇到什么问题,你是怎么解决的?...
查看>>
【校招面试 之 C/C++】第33题 C++ 11新特性(四)之STL容器
查看>>
Java替代C语言的可能性
查看>>
Mysql-2-数据库基础
查看>>
python把源代码打包成.exe文件
查看>>
PhotoshopCS5中将单位修改成百分比
查看>>
传统认知PK网络认知 刚子扯谈烤串认知
查看>>
字节数组java加密与解密
查看>>
矩形运算
查看>>
php 备份mysql数据库(joomla数据库可直接使用,其他数据库稍作修改即可)
查看>>
Design Pattern --- Strategy
查看>>
mui列表跳转到详情页优化方案
查看>>
一些简单有用的方法合集
查看>>
Neutron 架构 - 每天5分钟玩转 OpenStack(67)
查看>>
详解JS设计模式
查看>>
CPSR寄存器
查看>>