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]
随机推荐
- idea 找不到classpath 为resource下的xml
注入时不能自动找到在src/main/resources下的xml. @ContextConfiguration(locations = { "classpath:applicationCo ...
- CentOS7.4下的 JDK1.8 安装
一.卸载老的JDK 如果需要卸载OpenJDK,执行以下操作: [root@localhost ~]# rpm -e --nodeps tzdata-java-2014i-1.el7.noarch[r ...
- Python之IO模型
IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞 ...
- Python/Django-Web原理(一)
Python/Django-Web原理(一) websocket webSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML规范中被引用为TCP连接,作为基于TCP的套接字AP ...
- *args和**kwargs
#coding=utf8 __author__ = 'Administrator' # 当函数的参数不确定时,可以使用*args和**kwargs.*args没有key值,**kwargs有key值 ...
- Hive:insert into table 与 insert overwrite table 区别
创建测试表,来测试看看测试结果: create table test(name string,pwd string,createdate string)row format delimited fie ...
- javascript的变量声明、数据类型
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- VueJs(2)---VueJs开发环境的搭建和讲解index.html如何被渲染
VueJs开发环境的搭建和讲解初始框架 有关如何搭建vue.js框架我这看了一篇文章,自己也根据它进行搭建环境. 文章地址:vue.js2.0实战(1):搭建开发环境及构建项目 接下来对初始的框架进行 ...
- 使用javaMail实现简单邮件发送
一.首先你要用来发送邮件的qq邮箱需要开通pop3/smtp服务,这个可以百度一下就知道了 二.导入所需要的jar包,我使用的是maven添加依赖 <dependency> <gro ...
- css befroe after 尾类技术器
CSS counter计数器(content目录序号自动递增)详解 这篇文章发布于 2014年08月26日,星期二,15:54,归类于 css相关. 阅读 44148 次, 今日 11 次 by zh ...