第一在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. Python内置函数(5)——pow

    英文文档: pow(x, y[, z]) Return x to the power y; if z is present, return x to the power y, modulo z (co ...

  2. Hadoop学习笔记一(HDFS架构)

    介绍 Hadoop分布式文件系统(HDFS)设计的运行环境是商用的硬件系统.他和现存的其他分布式文件系统存在很多相似点.不过HDFS和其他分布式文件系统的区别才是他的最大亮点,HDFS具有高容错的特性 ...

  3. axure 预览"HTTP/1.1 302 Found"

    使用Axure编辑原型时,点击预览出现"HTTP/1.1 302 Found" 第一想到的就是重新安装Axure和检查原型文件是否损坏,验证后证明前Axure和.rp文件都是完好的 ...

  4. JMeter入门(03)多台JMeter联合测试

    一.配置各个节点 1.配置jmeter.properties # Remote Hosts - comma delimited#remote_hosts=localhost:1099,localhos ...

  5. 新概念英语(1-127)A famous actoress(女演员)

    A:Can you recognize that woman, Liz ?B:I think I can, Kate. It must be Karen Marsh, the actoress.A:I ...

  6. JSON(二)——JavaScript中js对象与JSON格式字符串的相互转换

    首先我们来看一下js中JSON格式的字符串 var JSONStr1 = "{\"name\" : \"张三\"}"; 注意以下的写法不是j ...

  7. jacascript 函数参数与 arguments 对象

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 调用函数时,实参和形参需要一一对应,但如果参数多了的话,会很苦恼: 我们可以用键值对(字面量对象)的方式传 ...

  8. Multipath在OpenStack中的faulty device的成因及解决(part 1)

    | 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.如有问题,可以邮件:wangxu198709@gmail.com 简介: Multip ...

  9. 逻辑运算符、三元运算符、for循环、stack(栈),heap(堆),方法区,静态域

    Lesson One 2018-04-17 19:58:39 逻辑运算符(用于逻辑运算,左右两边都是 true 或 false) 逻辑与-& 和 短路与-&& 区别: & ...

  10. python中创建实例属性

    虽然可以通过Person类创建出xiaoming.xiaohong等实例,但是这些实例看上除了地址不同外,没有什么其他不同.在现实世界中,区分xiaoming.xiaohong要依靠他们各自的名字.性 ...