第一在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>com.lgp</groupId>
<artifactId>maven_struts2</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven_struts2 Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties> <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.31</version>
</dependency>
</dependencies>
<build>
<finalName>maven_struts2</finalName>
</build>
</project>

导入包之后,有的电脑会报错,maven-update就好了

然后去web.xml那里注册struts2的过滤器

 <!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>Archetype Created 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>

然后就写struts.xml,记住放在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>
<!-- name:包名,可自定义 extends:继承 namespace:命名空间 -->
<!-- 定义常量 请求后缀 默认是.action -->
<!-- 但指定了之后 就必须写上后缀 -->
<constant name="struts.action.extension" value="action,do" />
<package name="helloworld" extends="struts-default" namespace="/">
<action name="helloworld_*" class="com.action.HelloWorldAction"
method="{1}">
<result name="{1}">{1}.jsp</result>
</action>
</package>
<package name="user" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="myInterceptor" class="com.interceptor.MyInterceptor"></interceptor>
<interceptor-stack name="mystack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="myInterceptor" />
<!-- 先进入默认拦截器,再到自定义拦截器 -->
</interceptor-stack>
</interceptors>
<action name="user_login" class="com.action.UserAction" method="login">
<interceptor-ref name="mystack"></interceptor-ref>
<result name="success">userinfo.jsp</result>
</action>
</package>
</struts>

action类

 package com.action;

 public class HelloWorldAction {
public String a() {
System.out.println("a..");
return "a";
} public String b() {
System.out.println("b..");
return "b";
} public String c() {
System.out.println("c..");
return "c";
} public String user() {
System.out.println("user..");
return "user";
}
} package com.action; import com.entiy.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack; public class UserAction extends ActionSupport {
private static final long serialVersionUID = -1417237614181805435L;
private String name;
private String pwd; public String login() {
System.out.println("login..");
System.out.println("name----" + name);
System.out.println("pwd----" + pwd);
// ValueStack vs=ActionContext.getContext().getValueStack();
ActionContext context = ActionContext.getContext();
ValueStack vs = context.getValueStack();
// 值栈的栈顶 User user = new User("张三", "张三的密码");
vs.push(user);
return SUCCESS;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
}
}

实体类

 package com.entiy;

 public class User {
private String name;
private String pwd; public User(String name, String pwd) {
this.name = name;
this.pwd = pwd;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

拦截器

 package com.interceptor;

 import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; /**
* 自定义拦截器
*
* @author SUMMER
*
*/
public class MyInterceptor extends AbstractInterceptor { /**
*
*/
private static final long serialVersionUID = 1L; /**
* 判断session有关的操作
*/
@Override
public String intercept(ActionInvocation actioninvocation) throws Exception {
System.out.println("我的拦截器开始...");
actioninvocation.invoke();
System.out.println("我的拦截器结束...");
// AOP 面向切面编程
return null;
} }

a.jsp/b.jsp/c.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>Insert title here</title>
</head>
<body>
aaaaaaaaaaaaaaaa
</body>
</html>

index.jsp

a没有后缀所以是错误的

 <%@ 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>Insert title here</title>
</head>
<body>
<a href="<%=request.getContextPath() %>/helloworld_a">a</a>
<br><br>
<a href="<%=request.getContextPath() %>/helloworld_b.do">b</a>
<br><br>
<a href="<%=request.getContextPath() %>/helloworld_c.action">c</a>
<br><br>
<a href="<%=request.getContextPath() %>/helloworld_user.action">用户</a>
</body>
</html>

user.jsp/userinfo.jsp

姓名分成3组

第二第三组是一对的,第一是混搭的,注意取值的方法

 <%@ 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>Insert title here</title>
</head>
<body>
<form action="<%=request.getContextPath() %>/user_login.do" method="post">
姓名:<input type="text" name="name">
<br><br>
密码:<input type="text" name="pwd">
<br><br>
<input type="submit" value="登录">
</form>
</body>
</html>
 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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>Insert title here</title>
</head>
<body>
姓名1:${name}
<br>密码1:<%=request.getParameter("pwd")%><br>
<br>姓名2:
<s:property value="[0].name" />
<br>密码2:${requestScope.pwd}
<br>姓名3:
<s:property value="[1].name" />
<br>密码3:
<s:property value="[1].pwd" />
<br>
</body>
</html>

SSH构造struts2项目的更多相关文章

  1. 项目:《ssh框架综合项目开发视频》-视频目录和第六天的EasyUI简单讲解

    4 练习使用技术: Struts2 + hibernate5.x + spring4.x + mysql数据库 1 crm:customer relational manager,客户关系管理 2 c ...

  2. 启动struts2项目出现classnotfound错误

    由于工作需求.需要了解struts2项目,前几天部署了一个struts2的demo,研究url的解析过程,昨天还是好好的,今天修改了一下web.xml文件,然后启动Tomcat就报错,错误如下: 严重 ...

  3. Struts2项目中使用Ajax报错

    在Struts2项目中使用Ajax向后台请求数据,当添加了json-lib-2.3-jdk15.jar和struts2-json-plugin-2.3.4.1.jar两个包时,在result中配置ty ...

  4. eclipse环境下基于已构建struts2项目整合spring+hibernate

    本文是基于已构建的struts2项目基础上整合 spring+hibernate,若读者还不熟悉struts2项目,请先阅读 eclipse环境下基于tomcat-7.0.82构建struts2项目 ...

  5. eclipse环境下基于tomcat-7.0.82构建struts2项目

    开山第一篇,毕业4个月目前接触最多的框架还是s2sh框架.... 具备完整的开发环境下,在eclipse下启动tomcat出现如下所示画面表示环境构建成功. 第一步:创建web项目,截图如下 此页面只 ...

  6. [ SSH框架 ] Struts2框架学习之一

    一.Struts2框架的概述 Struts2是一种基于MVC模式的轻量级Web框架,它自问世以来,就受到了广大Web开发者的关注,并广泛应用于各种企业系统的开发中.目前掌握 Struts2框架几乎成为 ...

  7. eclipse不能运行Struts2项目

    刚接触Struts2项目,本想写个HelloWorld上手,谁知道光eclipse配置tomcat就鼓捣一晚上,查阅各种资料. 项目刚开始报错: "java.lang.ClassNotFou ...

  8. jetty7.6运行struts2项目问题解决

    运行struts2项目报错:报错1:11:56:51,400  WARN Dispatcher:68 - Could not find action or result: /credit_public ...

  9. Struts2项目走向流转

    ----------------siwuxie095 Struts2 项目走向流转 1.HTTP 请求流转过程 2.配置文件连接点详解 [made by siwuxie095]

随机推荐

  1. kubernetes进阶(01)kubernetes的namespace

    一.Namespace概念 Namespace是对一组资源和对象的抽象集合,比如可以用来将系统内部的对象划分为不同的项目组或用户组. 常见的pods, services, replication co ...

  2. OAuth2.0学习(1-6)授权方式3-密码模式(Resource Owner Password Credentials Grant)

    授权方式3-密码模式(Resource Owner Password Credentials Grant) 密码模式(Resource Owner Password Credentials Grant ...

  3. css3中的动画 @keyframes animation

    动画的运用比较重要.接下来我希望针对我自己学习遇到的问题,再总结一下这个属性的使用方法. 创建一个动画: @keyframes 动画名 {样式} 引用自己创建的动画: animation:动画名  时 ...

  4. python当中的生成器

    最近身边的朋友都在问我迭代器是什么回事,经常跟大家一起讨论python的迭代器,一点点的我觉着自己有了更深一层的理解.我写下这篇文章,希望能对懵懵懂懂的好伙伴有些帮助~ 我也不是什么能人,难免说错一些 ...

  5. awk、变量、运算符、if多分支

    awk.变量.运算符.if多分支 awk: 语法 awk [options] 'commands' files option -F 定义字段分隔符,默认的分隔符是连续的空格或制表符 使用option中 ...

  6. POJ-2586 Y2K Accounting Bug贪心,区间盈利

    题目链接: https://vjudge.net/problem/POJ-2586 题目大意: MS公司(我猜是微软)遇到了千年虫的问题,导致数据大量数据丢失.比如财务报表.现在知道这个奇特的公司每个 ...

  7. Opencv在mac系统的安装与试用

    1.在mac终端内,使用brew安装opencv3,这时我的opencv被安装到/usr/local/Cellar/opencv3/3.2.0内. 2.新建xcode 项目,选择command lin ...

  8. 初识webgl--我的webgl学习第一课(基于threeJs)

    一,我为什么要学习webgl 一个偶然的机会,在和朋友的聊天过程中,听说了webgl,也许过去也看到过,但是没有特别在意过.原来,JavaScript也可以很好的渲染并在网页上显示三维动画,不用借助插 ...

  9. “百度杯”CTF比赛 九月场_SQL

    题目在i春秋ctf大本营 题目一开始就提醒我们是注入,查看源码还给出了查询语句 输入测试语句发现服务器端做了过滤,一些语句被过滤了 试了一下/**/.+都不行,后来才发现可以用<>绕过 接 ...

  10. 坑:JavaScript 中 操作符“==” 和“===” 的区别

    标题:JavaScript 中 操作符"==" 和"===" 的区别 记录一些很坑的区别: 1. '' == '0' // false 0 == '' // t ...