1.struts框架的使用 html
导入jar包java
1.commons-fileupload-1.2.jarweb
2. freemarker-2.3.15.jarapache
3.ognl-2.7.3.jar app
4.struts2-core-2.1.8.jar框架
5.xwork-core-2.1.6.jarjsp
2.配置web.xmlui
<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>
3.配置struts.xmlurl
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 岁月能够褪去记忆,却褪不去一路留下的欢声笑语。2017,但愿你们快乐继续,岁岁安怡 --> <!-- 一、name指定包名,包与包之间的名称不能重复; 二、namespace包命名空间,用于拼咱们访问包的 路径的,即URL。 不要忘记/线 三、extends指定包的继承关系的,这样咱们能够 方便的使用Struts2提供的默认配置。 --> <package name="demo" namespace="/hello" extends="struts-default"> <!-- 一、action是咱们本身要写的业务组件, 用于封装业务逻辑代码。 二、name是指定action的名称的, 该名称会用于咱们访问action。 同一包下能够有多个action, action名不能重复。 三、class指定该action对应的业务组件, 即咱们本身定义的业务代码类。 四、method指定要访问的方法名。 这个method能够省略,若省略 则默认调用execute方法。 五、要访问当前的Action,其URL以下: http://localhost:8080/Struts2demo01/hello/hello.action http://ip:port/project_name/package_namespace/action_name.action 注意:action名称的后缀.action能够省略的 --> <action name="hello" class="demo.HelloAction" method="sayHello"> <!-- 一、用result指定action处理完请求以后, 要去向的页面。 二、name指定result的名称, 用于访问该result, 同一个action下能够有多个result, 他们之间不能重名。 --> <result name="success"> /WEB-INF/jsp/helloStruts.jsp </result> <result name="error"> /WEB-INF/jsp/error.jsp </result> </action> </package> </struts>
4.写error.jsp 以及helloStruts.jspspa
<%@ 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> This is a Error! </body> </html>
<%@ 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> Hello World! </body> </html>
5.写HelloAction这个java类
package demo; public class HelloAction { /** * 该方法在struts.xml配置过, 与配置文件相对应。 */ public String sayHello() { System.out.println("Hello,Struts!"); /* * 返回的字符串与struts.xml中 定义的result对应,是用来找result的。 */ return "success"; } }
运行