概述
目标:
Struts的初步使用:
1、Struts的配置
依赖->导入jar包->maven的pom.xml
做配置->web.xml配置中央控制器->配置过滤器
框架配置文件->mvc.xml->struts.xml
开发->写子控制器继承ActionSupport
2、Struts动态方法调用
3、Struts的传参
自定义mvc:Modeldriver接口传参;
Struts:①、Modeldriver接口传参;
②、set方法传参;
③、通过对象属性传参;
4、Struts与Tomcat的集成
其中网不好可能会报的错:
服务跑不起来
解决方法:
①、首先将eclipse关闭;
②、找到本地仓库,将所有的jar全部删除;
③、再打开eclipse,让他自动下载jar;
④、如果还是不可以,换个网试试;
一、Struts
1、Struts的配置(写在Struts/pom.xml)
①、导jar包:Struts、Tomcat(做测试)
Struts:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
Tomcat:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
②、配置过滤器
路径(org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter)
Ctrl+Shift+T:查找
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
③、开发 放配置文件(Java Resources->src/main/resources)(相当于mvc.xml)
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 引入了Struts的默认配置 -->
<include file="struts-default.xml"></include>
<!-- 开发人员对Struts框架的基本配置 -->
<include file="struts-base.xml"></include>
<!-- 项目涉及的模块,分文件管理 -->
<include file="struts-sy.xml"></include>
</struts>
struts-sy.xml(包里面做配置)
2、Struts动态方法调用
①、struts-sy.xml
{1}代表第一个*
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="sy" extends="base" namespace="/sy">
<!-- {1}代表第一个* -->
<action name="/demo1_*" class="com.mwy.web.Demo1Action" method="{1}">
<result name="bookEdit">/bookEdit.jsp</result>
</action>
</package>
</struts>
两个*的写法:
第一个*代表调用方法;
第二个*代表跳哪个界面(一般使用一个*( {1}代表第一个*)的写法);
struts-sy.xml
<action name="/demo1_*_*" class="com.mwy.web.Demo1Action" method="{1}">
<result name="{2}">/{2}.jsp</result>
</action>
demo1.jsp(操作界面)
<a href="${pageContext.request.contextPath }/sy/demo1_add_bookEdit.action">增加</a>
②、Demo1Action
package com.mwy.web;
import com.opensymphony.xwork2.ActionSupport;
/**
*子控制器
*/
public class Demo1Action extends ActionSupport{
public String add() throws Exception {
System.out.println("add增加方法");
return "bookEdit";
}
public String del() throws Exception {
System.out.println("del删除方法");
return "bookEdit";
}
public String edit() throws Exception {
System.out.println("edit修改方法");
return "bookEdit";
}
}
③、demo1.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>
<h2>Struts动态方法调用</h2>
<a href="${pageContext.request.contextPath }/sy/demo1_add.action">新增</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_del.action">删除</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_edit.action">修改</a><br>
</body>
</html>
④、bookEdit.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>
<h1>——成功界面——</h1>
</body>
</html>
⑤、结果界面展示
3、Struts的传参
①、首先创建实体类
package com.mwy.entity;
public class User {
private String uid;
private String uname;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
@Override
public String toString() {
return "User [uid=" + uid + ", uname=" + uname + "]";
}
}
②、三种传参方法:
(1)、Modeldriver接口传参
Demo1Action
package com.mwy.web;
import com.mwy.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
*子控制器
*/
public class Demo1Action extends ActionSupport implements ModelDriven<User>{
private User user1=new User();
public String list() throws Exception {
System.out.println("list-----");
System.out.println(user1);
return "bookEdit";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user1;
}
}
demo1.jsp
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=001&&uname=mi">Modeldriver接口传参;</a><br>
点击第一个:
(2)、set方法传参
Demo1Action
package com.mwy.web;
import com.mwy.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
*子控制器
*/
public class Demo1Action extends ActionSupport implements ModelDriven<User>{
private User user1=new User();
//set传参
private String sex;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String list() throws Exception {
System.out.println("list-----");
System.out.println(user1);
System.out.println(sex);
return "bookEdit";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user1;
}
}
demo1.jsp
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?sex=-nv-">set方法传参</a><br>
点击第二个:
(3)通过对象属性传参
Demo1Action
package com.mwy.web;
import com.mwy.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
*子控制器
*/
public class Demo1Action extends ActionSupport implements ModelDriven<User>{
private User user1=new User();
//对象传参
private User user2;
public User getUser2() {
return user2;
}
public void setUser2(User user2) {
this.user2 = user2;
}
public String list() throws Exception {
System.out.println("list-----");
System.out.println(user1);
System.out.println(sex);
System.out.println(user2);
return "bookEdit";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user1;
}
}
demo1.jsp
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user2.uid=002&&user2.uname=mwy">通过对象属性传参</a><br>
点击第三个:
4、Struts与Tomcat的集成
两种方式:
①、类置类(偶尔用一下,用的不多)
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("age", 12);
package com.mwy.web;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.mwy.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
*子控制器
*/
public class Demo1Action extends ActionSupport implements ModelDriven<User>{
private User user1=new User();
//对象传参
private User user2;
public User getUser2() {
return user2;
}
public void setUser2(User user2) {
this.user2 = user2;
}
//set传参
private String sex;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String list() throws Exception {
System.out.println("list-----");
System.out.println(user1);
System.out.println(sex);
System.out.println(user2);
//类置类
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("age", 12);
return "bookEdit";
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user1;
}
}
demo1.jsp(随机点击一个)
<h2>Struts的传参</h2>
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=001&&uname=mi">Modeldriver接口传参;</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?sex=-nv-">set方法传参</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user2.uid=002&&user2.uname=mwy">通过对象属性传参</a><br>
②、实现接口
实现两个接口:ServletRequestAware、ServletResponseAware
重写方法:
@Override
public void setServletRequest(HttpServletRequest arg0) {
this.req=arg0;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.resp=arg0;
}
将
private HttpServletRequest req;
private HttpServletResponse resp;
提出来
Demo1Action
package com.mwy.web; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import com.mwy.entity.User; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; /** *子控制器 */ public class Demo1Action extends ActionSupport implements ModelDriven<User>,ServletRequestAware,ServletResponseAware{ private HttpServletRequest req; private HttpServletResponse resp; private User user1=new User(); //对象传参 private User user2; public User getUser2() { return user2; } public void setUser2(User user2) { this.user2 = user2; } //set传参 private String sex; public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String list() throws Exception { System.out.println("list-----"); System.out.println(user1); System.out.println(sex); System.out.println(user2); //类置类 HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("age", 12); req.setAttribute("hobby", "ddddddd"); return "bookEdit"; } //重写方法 @Override public void setServletRequest(HttpServletRequest arg0) { this.req=arg0; } @Override public void setServletResponse(HttpServletResponse arg0) { this.resp=arg0; } @Override public User getModel() { // TODO Auto-generated method stub return user1; } }
demo1.jsp(随机点击一个)
<h2>Struts的传参</h2>
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?uid=001&&uname=mi">Modeldriver接口传参;</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?sex=-nv-">set方法传参</a><br>
<a href="${pageContext.request.contextPath }/sy/demo1_list.action?user2.uid=002&&user2.uname=mwy">通过对象属性传参</a><br>
bookEdit.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>
<h1>——成功界面——</h1>
<h2>集成两种方式</h2>
①、类置类:${age }
②、${hobby }
</body>
</html>
成功界面
结束!!!
最后
以上就是开心花生为你收集整理的SSH之Struts入门的全部内容,希望文章能够帮你解决SSH之Struts入门所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复