Struts2.0学习---第一个程序
来源:互联网 发布:app数据查询 编辑:程序博客网 时间:2024/06/05 00:23
刚开始接触框架,怀着崇敬的心情去学习Struts2.0,然后就被其强大的功能,简洁的代码所吸引,然而一开始还是碰了不少壁,有很多细节需要掌握,不然要一直和bug打交道了。
我这个是Struts2.0框架,2.0以上的版本不保证可以正常运行。
导包的话,直接导入样例中最小的包的lib。
先在src下建个包action,里面一个继承ActionSupport类的ShowWords类。
package action;import com.opensymphony.xwork2.ActionSupport;public class ShowWords extends ActionSupport {private String name;private String words;@Overridepublic String execute() throws Exception {if("".equals(name)){return INPUT;}else{words="welcome"+name;return SUCCESS;}}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getWords() {return words;}public void setWords(String words) {this.words = words;}}
web.xml配置过滤器
<?xml version="1.0" encoding="UTF-8"?><web-app version="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"> <filter><filter-name>struts2.0</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter> <filter-mapping> <filter-name>struts2.0</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>structs.xml直接从最小的样例copy,中间做一些改变
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.il8n.encoding" value="UTF-8"/><package name="default" extends="struts-default"><action name="showWords" class="action.ShowWords"><result name="success">/showWords.jsp</result><result name="input">/inputWords.jsp</result></action></package></struts>
showWords.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'showWords.jsp' starting page</title> </head> <body> ${words } </body></html>inputWords.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'inputWords.jsp' starting page</title> </head> <body> <form action="showWords.action" method="post"> 您的昵称:<input type="text" name="name"/> <input type="submit" name="提交"/> </form> </body></html>
其中struts.xml中配置的name属性showWords,即是form表单action的属性。
程序运行流程先访问inputWords.jsp输入昵称,然后点击提交给struts.xml,struts.xml将加载class “action.ShowWords” 返回success,然后便调用showWords.jsp,显示信息。
0 0
- Struts2.0学习---第一个程序
- 学习 -- 使用Struts2实现第一个程序
- Struts2学习【1】第一个完整程序
- 第一个struts2程序
- 第一个struts2程序
- struts2第一个程序
- struts2 第一个程序
- 第一个struts2程序
- Struts2第一个程序
- 第一个struts2程序
- 第一个Struts2程序
- 第一个struts2程序
- 第一个struts2程序
- 第一个 Struts2 程序
- struts2第一个程序
- struts2第一个程序
- 第一个struts2程序
- 第一个Struts2程序
- 3.3 垃圾收集算法
- codeforces 682D(DP)
- Phpstorm配置git版本控制器
- Java后台学习之---cookie和session学习
- linux定时任务crontab自动备份mysql数据库(ssh/php)
- Struts2.0学习---第一个程序
- android开发实用工具
- LightOJ1285->极角排序
- 洛谷 P1024 [NOIP2001 T1] 一元三次方程求解
- 3.4 HotSpot算法实现
- (Leetcode)215.Kth Largest Element in an Array(medium)
- IntelliJ IDEA 使用教程
- POJ 3667
- Android中的Adapter 详解(三)