Struts2入门
Posted chenjiahao9527
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2入门相关的知识,希望对你有一定的参考价值。
一.struts框架得到环境搭建
首先我们建一个maven项目(里面有很多细节问题可以看 https://www.cnblogs.com/chenjiahao9527/p/11079245.html)
这次在pom.xml加下面的代码
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.13</version> </dependency>
然后我们把下面配置文件导入项目中的src/amin/resources中
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> <include file="struts-default.xml"></include><!-- 原有的配置文件 --> <include file="struts-base.xml"></include><!-- 基础的配置文件 --> <include file="struts-sy.xml"></include> </struts>
struts-base.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> <constant name="struts.i18n.encoding" value="UTF-8" /><!-- 编码格式 --> <constant name="struts.devMode" value="true" /><!-- 动态方法调用 --> <constant name="struts.configuration.xml.reload" value="true" /><!-- 不重启项目修改代码立即生效 --> <constant name="struts.i18n.reload" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="true" /><!-- 开起动态方法调用 --> <package name="base" extends="struts-default" abstract="true"> <global-allowed-methods>regex:.*</global-allowed-methods> </package> </struts>
struts-sy.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> <!-- 这包能更加细分action的归类 --> <package name="sy" extends="base" namespace="/sy"> </action> </package> </struts>
配置web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <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> </web-app>
二.struts的动态方法调用、struts传参(jsp和后台的互传)
1、动态动态方法调用
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> <h3>动态方法调用</h3> <a href="$pageContext.request.contextPath/sy/demo_add.action">新增</a> <a href="$pageContext.request.contextPath/sy/demo_del.action">删除</a> </body> </html>
在struts-sy.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> <!-- 这包能更加细分action的归类 --> <package name="sy" extends="base" namespace="/sy"> <action name="/demo_*" class="com.chenjiahao.web.HelloAction" method="1"> <result name="rs">/rs.jsp</result> </action> </package> </struts>
java代码
//动态方法的调用
public class HelloAction() public String add() System.out.println("调用add方法......."); return "rs"; public String del() System.out.println("调用del方法......."); return "rs";
这样就可以调用动态方法了
2、jsp传递参数到后台,后台如何接受
先建一个实体类
package com.chenjiahao.emtity; public class Cal private String num1; private String num2; public String getNum1() return num1; public void setNum1(String num1) this.num1 = num1; public String getNum2() return num2; public void setNum2(String num2) this.num2 = num2; @Override public String toString() return "Cal [num1=" + num1 + ", num2=" + num2 + "]";
implements modelDrivern
set/get
类实例.属性名
public class HelloAction implements ModelDriven<Cal> private HttpServletRequest request; private Cal cal1=new Cal(); private Cal cal2; private String sex; public Cal getCal2() return cal2; public void setCal2(Cal cal2) this.cal2 = cal2; public String getSex() return sex; public void setSex(String sex) this.sex = sex; /** * implements ModelDriven 接收参数值 * @return */ public String accept1() System.out.println("cal1:"+cal1); return "rs"; /** * 类实例.属性名 接收参数值 * @return */ public String accept2() System.out.println("cal2:"+cal2); return "rs"; /** * set/get 接收参数值 * @return */ public String accept3() System.out.println(sex); return "rs";
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> <h3>后台接收jsp传递参数的三种方法</h3> <a href="$pageContext.request.contextPath/sy/demo_accept1.action?num1=20&&num2=5">accept1</a> <a href="$pageContext.request.contextPath/sy/demo_accept2.action?cal2.num1=20&&cal2.num2=5">accept2</a> <a href="$pageContext.request.contextPath/sy/demo_accept3.action?sex=nv">accept3</a> </body> </html>
结果:
3、后台传递到jsp的方式
set/get定义的属性是可以接受到的不需要任何操作
直接在jsp用el表达式
req.set.....
public class HelloAction ServletRequestAware
private HttpServletRequest request;
public String accept1() System.out.println("cal1:"+cal1); /** *后台传递到jsp的方式 *set/get定义的属性是可以接受到的 *req.set..... */ //注入耦合 // request.setAttribute("cal1", cal1); //非注入耦合 HttpServletRequest request2 = ServletActionContext.getRequest(); request2.setAttribute("cal1", cal1); // 非注入解耦(这种方式一般没人用,因为记不住context的实现类路径) // ActionContext context = ActionContext.getContext(); // context.get("XXXX"); return "rs";
以上是关于Struts2入门的主要内容,如果未能解决你的问题,请参考以下文章