struts2入门Demo示例
程序员文章站
2023-01-29 11:10:51
本文讲述了struts2入门demo示例。分享给大家供大家参考。具体如下:
1.新建web project, 名称:struts2demo;
2.建立一个用户库...
本文讲述了struts2入门demo示例。分享给大家供大家参考。具体如下:
1.新建web project, 名称:struts2demo;
2.建立一个用户库struts2, 包含最少的struts2的最少的6个jar文件;
其实呢, 对于myeclipse8以上来说, 是不必须的, 因为它直接支持struts2了.不需要另外导包.
3.用build path将struts2的库加进来;
4.在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" > <!-- struts2的配置 --> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
5.建立action
package com.yenange.action; import com.opensymphony.xwork2.action; public class loginaction implements action { private string uname; //表单中的姓名 private string upass; //表单中的密码 //执行方法 @override public string execute() throws exception { system.out.println("姓名:"+uname); system.out.println("密码:"+upass); if (uname.equals("leaf") && upass.equals("leaf")) { return "success"; } return "error"; } //记住, 要有get和set方法 public string getupass() { return upass; } public void setupass(string upass) { this.upass = upass; } public string getuname() { return uname; } public void setuname(string uname) { this.uname = uname; } }
6.三个页面文件:
index.jsp:
<%@ page language="java" import="java.util.*" pageencoding="gbk"%> <html> <head> <title>my jsp 'index.jsp' starting page</title> </head> <body> <form action="login"> 用户名:<input type="text" name="uname"><br> 密 码:<input type="text" name="upass"><br> <input type="submit" value="登陆" /> </form> </body> </html>
welcome.jsp:
<%@ page language="java" import="java.util.*" pageencoding="gbk"%> <html> <head> <title>my jsp 'welcome.jsp' starting page</title> </head> <body> welcome struts2 </body> </html>
fail.jsp:
<%@ page language="java" import="java.util.*" pageencoding="gbk"%> <html> <head> <title>my jsp 'welcome.jsp' starting page</title> </head> <body> 出错! </body> </html>
7.配置struts.xml. (对于myeclipse6来说, 只能从别的地方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> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="com.yenange.action.loginaction"> <result name="success">/welcome.jsp</result> <result name="error">/fail.jsp</result> </action> </package> <!-- add packages here --> </struts>
8.到此完成.可以运行一下了, 是不是很简单?
下面是取得request, session, application这些东西的方法.
package com.yenange.action; import java.util.map; import javax.servlet.servletcontext; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; import org.apache.struts2.servletactioncontext; import org.apache.struts2.interceptor.servletrequestaware; import org.apache.struts2.interceptor.sessionaware; import org.apache.struts2.util.servletcontextaware; import com.opensymphony.xwork2.action; public class loginaction implements action,servletrequestaware,sessionaware,servletcontextaware { private string uname; //表单中的姓名 private string upass; //表单中的密码 httpservletrequest request; //常用的request map session; //常用的session,有不同, 但是不需要管 servletcontext application; //常用的application //第二种取的方法; httpservletrequest request2 = servletactioncontext.getrequest(); httpservletresponse response2 = servletactioncontext.getresponse(); httpsession session2 = request2.getsession(); @override public void setservletrequest(httpservletrequest req) { this.request=req; } @override public void setsession(map session) { this.session=session; } @override public void setservletcontext(servletcontext application) { this.application=application; } //执行方法 public string execute() throws exception { if (uname.equals("leaf") && upass.equals("leaf")) { request.setattribute("username", uname); return "success"; } request.setattribute("accesserror", uname+"这个用户或密码不正确!"); return "error"; } //记住, 要有get和set方法 public string getupass() { return upass; } public void setupass(string upass) { this.upass = upass; } public string getuname() { return uname; } public void setuname(string uname) { this.uname = uname; } }
这些只是初步了, 后面再学习一些关于标签, 动态方法之类的东西.
希望本文所述对大家的struts程序设计有所帮助。
推荐阅读
-
Struts2单选按钮详解及枚举类型的转换代码示例
-
jQuery插件FusionCharts绘制的2D帕累托图效果示例【附demo源码】
-
jQuery插件FusionCharts实现的2D柱状图效果示例【附demo源码下载】
-
jQuery插件echarts实现的单折线图效果示例【附demo源码下载】
-
jQuery插件echarts实现的去掉X轴、Y轴和网格线效果示例【附demo源码下载】
-
jQuery插件Echarts实现的双轴图效果示例【附demo源码下载】
-
jQuery插件echarts实现的循环生成图效果示例【附demo源码下载】
-
jQuery插件echarts实现的多柱子柱状图效果示例【附demo源码下载】
-
jQuery插件echarts实现的多折线图效果示例【附demo源码下载】
-
spring security入门demo