Struts2学习(一)
2016-07-25 13:49
381 查看
struts2 就是 web层开发框架,符合MVC模式
入门程序
创建web工程
导入jar包
下载struts2的jar包 struts-2.3.15.1-all 版本.
注意:在struts2开发,一般情况下最少导入的jar包,去apps下的struts2-blank示例程序中copy
创建index.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>Hello Struts2</h1> </body> </html>
创建hello.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>Hello Struts2</h1> </body> </html>
对struts2框架进行配置
web.xml
web.xml文件中配置前端控制器(核心控制器)-----就是一个Filter
目的:是为了让struts2框架可以运行。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>struts2_01</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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>
struts2.xml
创建一个struts.xml配置文件 ,这个是struts2框架配置文件。
目的:是为了struts2框架流程可以执行。
名称:struts.xml
位置:src下(classes下)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="hello" class="com.cxz.HelloAction" method="say"> <result name="good">/hello.jsp</result> </action> </package> </struts>
创建一个HelloAction类
要求,在HelloAction类中创建一个返回值是String类型的方法,注意,无参数。
package com.cxz; public class HelloAction { public String say() { System.out.println("Hello action say method"); return "good"; } }
在struts.xml文件中配置HelloAction
<package name="default" namespace="/" extends="struts-default"> <action name="hello" class="com.cxz.HelloAction" method="say"> <result name="good">/hello.jsp</result> </action> </package>
在index.jsp中添加连接,测试
<a href="${pageContext.request.contextPath }/hello.action">访问Struts2入门</a>
运行
http://localhost:8080/struts2_01/
相关文章推荐
- Java提高篇——通过分析 JDK 源代码研究 Hash 存储机制
- Scanner类中的nextLine()产生的换行符问题
- java中的内部内总结
- Description Resource Path Location Type Java compiler level does not match the version of the instal
- Struts2约定优于配置
- dex分包 轻松解决解决65535的限制
- Java自动内存管理机制(二) 运行时数据区
- java序列化
- Java解析xml的4种经典方法
- Java 带index信息排序(包含集合框架的使用)
- Spring入门
- Maven项目在Eclipse中调试 Debug
- 在SSH框架中使用Spring的好处
- spring AOP @Around @Before @After 区别
- 在Java中如何遍历Map对象
- spring mvc 中使用spring的validator
- (8)Spring的事务控制
- 在java中实现ctrl+z结束输入
- XML的增删改查
- java多线程下如何调用一个共同的内存单元(调用同一个对象)