eclipse 搭建Struts
来源:互联网 发布:中国对外援助知乎 编辑:程序博客网 时间:2024/05/21 21:02
一. 新建一个工程
1. 新建工程
2. 在WebContent/WEB-INF下新建web.xml
web.xml:
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3. 在WebContent下新建index.jsp
工程截图:
4. 运行。浏览器中输入http://localhost:8080/test/即可刷出主页。
5.
二. Struts2框架的简单搭建 Struts1
找到所需的jar包拷到WebContent/WEB-INF/lib下
struts2-core-2.x.x.jar : Struts 2框架的核心类库
xwork-2.x.x.jar : XWork类库,Struts 2在其上构建
ognl-2.6.x.jar : 对象图导航语言(Object Graph Navigation Language),Struts 2框架使用的一种表达式语言
freemarker-2.3.x.jar : Struts 2的UI标签的模板使用FreeMarker编写
commons-logging-1.1.x.jar: ASF出品的日志包,Struts 2框架使用这个日志包来支持Log4J和JDK 1.4+的日志记录。
Commons-fileupload
不同情况下导包不同
2.在web.xml文件中配置Struts2的过滤器
web.xml:
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3. 建立动作类HelloWorldAction类如下:
package com.cn;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private Stringmessage;
public String getMessage() {
returnmessage;
}
public void setMessage(String message) {
this.message = message;
}
public String execute()throws Exception{
this.message ="我的第一个struts2应用";
return"success";
}
}
4. 配置struts2的专用文件struts.xml ,FilterDispatcher过滤器在初始化
时将会在WEB-INF/classes下寻找该文件
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="mypackage"namespace="/test"extends="struts-default">
<actionname="helloworld"class="com.cn.HelloWorldAction"method="execute">
<resultname="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
</struts>
6. 新建hello.jsp
<%@ page language="java"pageEncoding="UTF-8"%>
<!DOCTYPEHTMLPUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<title>第一个struts2应用</title>
</head>
<body>
${message}<br>
</body>
</html>
工程结构:
6.运行。浏览器输入http://localhost:8080/Struts/test/helloworld.action
或者http://localhost:8080/Struts/test/helloworld
三. 工程换名
除了根文件夹换名,还有工程-右键-Properties-Web Project Settings Context root 也需要改
四. 使用通配符定义action Struts2
Struts.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="mypackage"namespace="/test"extends="struts-default">
<actionname="helloworld_*"class="com.cn.HelloWorldAction"method="{1}">
<resultname="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
</struts>
HelloWorldAction.java
package com.cn;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private Stringmessage;
public String getMessage() {
returnmessage;
}
public void setMessage(Stringmessage) {
this.message = message;
}
public String execute()throws Exception{
this.message ="我的第一个struts2应用";
return"success";
}
public String other()throws Exception{
this.message="第二个方法";
return"success";
}
浏览器访问结果
五.为Action的属性注入值
struts.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="mypackage"namespace="/test"extends="struts-default">
<actionname="helloworld"class="com.cn.HelloWorldAction"method="{1}">
<paramname="message">哈哈</param>
<resultname="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
</struts>
HelloWorldAction.java
package com.cn;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private Stringmessage;
public String getMessage() {
returnmessage;
}
public void setMessage(String message) {
this.message = message;
}
public String execute()throws Exception{
//this.message = "我的第一个struts";
return"success";
}
}
运行结果
上面通过<param>节点为action的message属性注入“哈哈”
六.全局跳转
当多个action中都使用到了相同result,这时我们应该把result定义为全局结果。
<package ....>
<global-results>
<resultname="message">/message.jsp</result>
</global-results>
</package>
七.校验程序 Struts3
web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Struts.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//Apache Software Foundation//DTD Struts Configuration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="aa"namespace="/aa"extends="struts-default">
<actionname="login*"class="com.action.PersonAction"method="{1}">
<resultname="input">/index.jsp</result>
<resultname="success">/success.jsp</result>
</action>
</package>
</struts>
PersonAction.java
package com.action;
import java.util.regex.Pattern;
importcom.opensymphony.xwork2.ActionContext;
importcom.opensymphony.xwork2.ActionSupport;
public class PersonAction extendsActionSupport{
privateString username;
privateString mobile;
publicString getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username= username;
}
publicString getMobile() {
returnmobile;
}
publicvoid setMobile(String mobile) {
this.mobile= mobile;
}
publicString update(){
ActionContext.getContext().put("message","更新成功");
return"success";
}
publicString save(){
ActionContext.getContext().put("message","保存成功");
return"success";
}
publicvoid validate(){
if(this.username==null||"".equals(this.username.trim())){
this.addFieldError("username","用户名不能为空");
}
if(this.mobile==null||"".equals(this.mobile.trim())){
this.addFieldError("mobile","手机号不能为空");
}else{
if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile.trim()).matches()){
this.addFieldError("mobile","手机号的格式不正确");
}
}
}
}
index.jsp
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@tagliburi="/struts-tags"prefix="s"%>
<body>
<s:fielderror></s:fielderror>
<formaction="aa/loginupdate"method="post">
用户名:<inputtype=textname="username"/>不能为空
<s:fielderror><s:param>username</s:param></s:fielderror></br>
手机号:<inputtype=textname="mobile"/>不能为空,并要符合手机号的格式1,3/5/8,后面是9个数字
<s:fielderror><s:param>mobile</s:param></s:fielderror><br/>
<inputtype=submitvalue="确定"/>
</form>
</body>
</html>
工程结构
总结
1. namespace:包的namespace属性用于定义该包的命名空间。该属性可以不配置,对本例而言,如果不指定该属性,默认的命名空间为“”(空字符串)。
默认命名空间:namespace=“”即配置是省略;
根命名空间: namespace=“/”;
struts.xml配置文件中,当默认命名空间中存在与根命名空间同名的Action时,根命名空间的配置信息优先级高于默认命名空间中的配置信息。
2. <package name="itclass"namespace="/test" extends="struts-default">
<action name="helloworld"class=“com.cn.HelloWorldAction">
<resultname="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
访问struts2中的action的URL路径由两部份组成:包的命名空间+action的名称,例如访问上面例子中名为helloworld的Action的URL路径为:/test/helloworld (注意:完整路径为:http://localhost:端口/内容路径/test/helloworld.action)。
如果没有为action指定class,默认是ActionSupport。而ActionSupport的execute() 方法默认处理就是返回一个success字符串。method属性用于指定action中的那个方法,如果没有指定默认执行action中的execute() 方法
3. 在大部分应用里,随着应用规模的增加,系统中Action数量也大量增加,导致struts.xml配置文件变得非常臃肿。为了避免struts.xml文件过于庞大、臃肿,提高struts.xml文件的可读性,我们可以将一个struts.xml配置文件分解成多个配置文件,然后在struts.xml文件中包含其他配置文件。下面的struts.xml通过<include>元素指定多个配置文件:
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<includefile="struts-user.xml"/>
<includefile="struts-order.xml"/>
</struts>
通过这种方式,我们就可以将Struts2的Action按模块配置在多个配置文件中。
4. 一旦做了validate的验证。 那么所以流经这个action类的请求都将经历一次验证
八.请求参数接收
在Action类中定义与请求参数同名属性,便能够使struts2自动接收请求参数并赋予同名属性。
采用基本类型接收请求参数(get/post)
在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名属性
index.jsp
<ahref="aa/loginupdate?username=baojie">hah</a>
PersonAction.java
public String update(){
System.out.println(username);
ActionContext.getContext().put("message","更新成功");
return"success";
}
采用复合类型接收请求参数
请求路径:http://localhost:8080/test/view.action?product.id=78
public class ProductAction {
private Product product;
public void setProduct(Product product) { this.product = product; }
public Product getProduct() {return product;}
}
Struts2首先通过反射技术调用Product的默认构造器创建product对象,然后再通过反射技术调用product中与请求参数同名的属性的setter方法来获取请求参数值。
九.自定义拦截器 Struts4
Web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Struts.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="aa"namespace="/test"extends="struts-default">
<actionname="login"class="com.cn.LoginAction">
<resultname="input">/login.jsp</result>
<resultname="error">/error.jsp</result>
<resultname="success">/welcome.jsp</result>
</action>
<actionname="query"class="com.cn.LoginAction"method="query">
<resultname="success">/query.jsp</result>
</action>
</package>
<packagename="bb"namespace="/test"extends="struts-default">
<interceptors>
<interceptorname="permission"class="com.cn.MyInter"/>
<interceptor-stackname="permissionStack">
<interceptor-refname="defaultStack"/>
<interceptor-refname="permission"/>
</interceptor-stack>
</interceptors>
<default-interceptor-refname="permissionStack"/>
<actionname="show"class="com.cn.LoginAction"method="show">
<resultname="login">/login.jsp</result>
<resultname="success">/show.jsp</result>
</action>
<actionname="add"class="com.cn.LoginAction"method="add">
<resultname="login">/login.jsp</result>
<resultname="success">/add.jsp</result>
</action>
</package>
</struts>
LoginAction.java
package com.cn;
importcom.opensymphony.xwork2.ActionContext;
importcom.opensymphony.xwork2.ActionSupport;
public class LoginAction extendsActionSupport{
privateString username;
privateString password;
publicString getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username= username;
}
publicString getPassword() {
returnpassword;
}
publicvoid setPassword(String password) {
this.password= password;
}
publicString execute() throws Exception{
System.out.println("username:"+username+"password:"+password);
if(username==""|| username==null ||password=="" ||password==null){
returnINPUT;
}
if(getUsername().equals("sunli")&&getPassword().equals("111")){
//通过ActionContext对象访问Web使用的Session
ActionContext.getContext().getSession().put("user",getUsername());
ActionContext.getContext().getSession().put("pass",getPassword());
returnSUCCESS;
}
else{
returnERROR;
}
}
publicString add(){
System.out.println("add");
returnSUCCESS;
}
publicString show(){
System.out.println("show");
returnSUCCESS;
}
publicString query(){
returnSUCCESS;
}
}
MyInter.java
package com.cn;
importcom.opensymphony.xwork2.ActionContext;
importcom.opensymphony.xwork2.ActionInvocation;
importcom.opensymphony.xwork2.interceptor.Interceptor;
public class MyInter implements Interceptor{
@Override
publicvoid destroy() {
//TODO Auto-generated method stub
System.out.println("destory");
}
@Override
publicvoid init() {
//TODO Auto-generated method stub
System.out.println("init");
}
@Override
publicString intercept(ActionInvocation invocation) throws Exception {
//TODO Auto-generated method stub
System.out.println("intercept");
Objectuser=ActionContext.getContext().getSession().get("user");
if(user!=null&&user.equals("sunli")){
Stringresult=invocation.invoke();
System.out.println("interceptfinish");
returnresult;
//返回所拦截方法的返回值,如要拦截动作类中的方法Add则返回Add方法的success
}
ActionContext.getContext().put("tip","您还没登陆,请先登录");
return"login";
}
}
Login.jsp
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert titlehere</title>
</head>
<body>
<formaction="test/login.action"method="post">
用户名:<inputtype=textname="username"/></br>
密 码:<inputtype=passwordname="password"/><br/>
<inputtype=submitvalue="提交"/>
</form>
<%
if (request.getAttribute("tip")!=null)
{
out.print(request.getAttribute("tip").toString());
}
%>
</body>
</html>
Welcome.jsp
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert titlehere</title>
</head>
<body>
欢迎,${sessionScope.user},您已经登录! ${sessionScope.pass}
<ahref="test/query.action">query</a><br/>
<ahref="test/show.action">show</a><br/>
<ahref="test/add.action">add</a><br/>
</body>
</html>
Add.jsp
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC "-//W3C//DTD HTML4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert titlehere</title>
</head>
<body>
show success!
<ahref="login.jsp">return login</a>
</body>
</html>
工程结构:
- eclipse 搭建Struts
- Eclipse搭建struts开发环境
- 用eclipse搭建struts开发环境
- Eclipse下struts框架的简单搭建
- Eclipse下搭建struts、spring、hibernate开发环境 搭建环境
- Struts+Spring+Hibernate之搭建Eclipse + MyEclipse 开发环境
- Eclipse下搭建struts、spring、hibernate开发环境
- 练手在eclipse中搭建spring+struts+hibernate开发框架
- Eclipse下搭建struts、spring、hibernate开发环境
- 在Eclipse中搭建Struts框架过程详解
- 用eclipse搭建ssh框架 struts spring hibernate
- 用eclipse搭建SSH(struts+spring+hibernate)框架
- Eclipse + Struts
- struts+eclipse
- 在Eclipse上搭建SSH框架(Struts 2.5.1 + Spring 4.3.0 + Hibernate 4.3.11)
- 搭建struts 2.0 环境
- 如何搭建struts平台
- 第一课 struts搭建
- HDU 4283 You Are the One 区间dp
- ztree学习之异步加载节点
- vmstat :侦测系统资源变化
- 网络传输流量控制策略小结
- MATLAB下的程序调试
- eclipse 搭建Struts
- 开始写博客
- 在arcgis的help中学习方法
- 跨时钟域的寄存器访问
- Wikilo 1191线段树区间修改单点查询
- 黑马程序员---指针函数 函数指针 指针数组 解析
- ubuntu 安装 openssh-server 直接使用SecureCRT作为ubuntu的远程终端
- C语言之路
- poj3744之矩阵快速幂+概率DP