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]
随机推荐
- Python内置函数(62)——exec
英文文档: exec(object[, globals[, locals]]) This function supports dynamic execution of Python code. obj ...
- 【JavaScript中typeof、toString、instanceof、constructor与in】
JavaScript中typeof.toString.instanceof.constructor与in JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行 ...
- 新概念英语(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 ...
- T410升级笔记
T410 win7 旗舰版 32 sp1 三星 DDR3 1066 mhz core i5 M 540 2.53GHZ 双核 日立 HTS725032A9A364 320G/7200转/分 sa ...
- Linux实战案例(3)创建和删除用户
建用户: adduser phpq //新建phpq用户passwd phpq //给php ...
- 使用nodeJS的 crypto模块来为你的密码hash加盐
这篇文章将向你解释如何使用Node.js的Crypto模块对你的密码进行加盐hash.在这里,我们将不会对不懂的密码存储方式进行详细的比较.我们将要做的是知道在Node.js中使用加盐hash在进行密 ...
- python Django之文件上传
python Django之文件上传 使用Django框架进行文件上传共分为俩种方式 一.方式一 通过form表单进行文件上传 #=================================== ...
- Django快速入门
Django 是用 Python 写的一个自由和开放源码 web 应用程序框架.web框架是一套组件,能帮助你更快.更容易地开发web站点.当你开始构建一个web站点时,你总需要一些相似的组件:处理用 ...
- hdu1789 Doing Homework again---(经典贪心)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1789 题目大意: 给出N个作业的截至日期,和N个作业不交所扣掉的分数,要求输出扣除分数做少的方案. ...
- hdu1003 Max Sum---最大子段和+记录开始结束点
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意: 求最大子段和,并且输出最大子段和的起始位置和终止位置. 思路: 根据最大子段和基本 ...