SSH构造struts2项目
第一在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项目的更多相关文章
- 项目:《ssh框架综合项目开发视频》-视频目录和第六天的EasyUI简单讲解
4 练习使用技术: Struts2 + hibernate5.x + spring4.x + mysql数据库 1 crm:customer relational manager,客户关系管理 2 c ...
- 启动struts2项目出现classnotfound错误
由于工作需求.需要了解struts2项目,前几天部署了一个struts2的demo,研究url的解析过程,昨天还是好好的,今天修改了一下web.xml文件,然后启动Tomcat就报错,错误如下: 严重 ...
- Struts2项目中使用Ajax报错
在Struts2项目中使用Ajax向后台请求数据,当添加了json-lib-2.3-jdk15.jar和struts2-json-plugin-2.3.4.1.jar两个包时,在result中配置ty ...
- eclipse环境下基于已构建struts2项目整合spring+hibernate
本文是基于已构建的struts2项目基础上整合 spring+hibernate,若读者还不熟悉struts2项目,请先阅读 eclipse环境下基于tomcat-7.0.82构建struts2项目 ...
- eclipse环境下基于tomcat-7.0.82构建struts2项目
开山第一篇,毕业4个月目前接触最多的框架还是s2sh框架.... 具备完整的开发环境下,在eclipse下启动tomcat出现如下所示画面表示环境构建成功. 第一步:创建web项目,截图如下 此页面只 ...
- [ SSH框架 ] Struts2框架学习之一
一.Struts2框架的概述 Struts2是一种基于MVC模式的轻量级Web框架,它自问世以来,就受到了广大Web开发者的关注,并广泛应用于各种企业系统的开发中.目前掌握 Struts2框架几乎成为 ...
- eclipse不能运行Struts2项目
刚接触Struts2项目,本想写个HelloWorld上手,谁知道光eclipse配置tomcat就鼓捣一晚上,查阅各种资料. 项目刚开始报错: "java.lang.ClassNotFou ...
- jetty7.6运行struts2项目问题解决
运行struts2项目报错:报错1:11:56:51,400 WARN Dispatcher:68 - Could not find action or result: /credit_public ...
- Struts2项目走向流转
----------------siwuxie095 Struts2 项目走向流转 1.HTTP 请求流转过程 2.配置文件连接点详解 [made by siwuxie095]
随机推荐
- 如何在Java中避免equals方法的隐藏陷阱
摘要 本文描述重载equals方法的技术,这种技术即使是具现类的子类增加了字段也能保证equal语义的正确性. 在<Effective Java>的第8项中,Josh Bloch描述了当继 ...
- maven常见问题处理(3-4)配置代理服务器
有的公司基于安全因素考虑,要求员工使用通过安全认证的代理访问因特网. 这时就需要为Maven配置HTTP代理. 在目录~/.m2/setting.xml文件中编辑如下(如果没有该文件,则复制$M2_H ...
- matlab 对tif数据高程图的处理分析
temp=z(101:2200,101:2200) 根据图像属性可得此为2300*2300的tif图像,由于需要将其划分为9宫格,所以begin点设置为101,end点设置为2200,temp转化为可 ...
- bootstrap表格 之多选数据的获取
使用表格的时候经常会用到多选的功能,比较常用,下面写一个小Dome记录一下 如下:单击批量删除按钮之后,需要获取选中行数据,传值到后台进行处理 一.获取选择行的数据 btnplDel是按钮id:tab ...
- nohup 与 & 的区别
nohup -- invoke a utility immune to hangups : 运行命令忽略挂起信号 & 是指后台运行: nohup 的功能和& 之间的功能并不相同.其中, ...
- python/SQLAchemy
python/SQLAchemy SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数 ...
- 云计算 IaaS,SaaS,PaaS的区别?一个通俗易懂的吃货文章
来自一篇吃货文章了: ———————————————————— <img src="https://pic2.zhimg.com/a55676f8e1b084a398f8cd5 ...
- Canvas绘制五角星
from tkinter import * import math as m root = Tk() w = Canvas(root, width=200, height=100, backgroun ...
- Java基础之程序流程控制
Java中的程序流程控制 Java中的程序流程分为三种结构:①顺序结构:②分支结构:③循环结构 一.顺序结构 Java中定义成员变量的时候,采用的是前向引用,也就是后面的变量可以引用之前定义好的变量. ...
- JavaScript实现轮播图(隔3秒显示一张图)
<!DOCTYPE html><html> <head> <script> var time; function init(){ //获取div里面的东 ...