Struts2学习之路_1_登录
2014-04-29 09:16
120 查看
PS:因为这是第一次贴Struts2的代码,所以把Struts.xml和web.xml我都会贴出来,之后如果有大的改动我才会继续吧这个两个xml贴出来。
1、代码
1.1、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</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>
1.2、Struts.xml
<?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> <!-- 原始的,可以用来参考 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results> <result name="error">/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error"/> </global-exception-mappings> <action name="index"> <result type="redirectAction"> <param name="actionName">HelloWorld</param> <param name="namespace">/example</param> </result> </action> </package> --> <constant name="struts.devMode" value="true" /> <!-- 如果value=true 开启开发模式,可以改完xml里面的东西就能自动刷新,不需要重新部署服务器 --> <package name="default" extends="struts-default"> <action name="LoginAction_2014_4_28" class="com.god.action.LoginAction_2014_4_28"> <result name="success">/2014_4_28_welcome_lx_01.jsp</result> <result name="input">/2014_4_28_login_lx_01.jsp</result> </action> </package> <include file="example.xml"/> <!-- Add packages here --> </struts>
1.3、两个页面代码
1.3.1、2014_4_28_login_lx_01.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP '2014_4_28_login_lx_01.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <center> <div> </div> <h3>这是一个简单的Struts 2应用</h3> <br/><hr/> <form action="LoginAction_2014_4_28.action" method="post"> 用户名:<input name="uname" type="text"/><br/> 密码:<input name="upasswd" type="text"/><br/> <input type="submit" value="提交"/> </form> </center> </body> </html>
1.3.1、2014_4_28_welcome_lx_01.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP '2014_4_28_welcome_lx_01.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body><center> <div> </div> <h3>一个简单的Struts 2应用</h3> <br/><hr/> ${uname}欢迎您! </center> </body> </html>
1.4action代码
package com.god.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction_2014_4_28 extends ActionSupport { org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter d; private String uname; private String upasswd; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpasswd() { return upasswd; } public void setUpasswd(String upasswd) { this.upasswd = upasswd; } public String execute(){// 类似于servlet的doGet和doPost方法 if(uname.equals("fang")&&upasswd.equals("123")){ return SUCCESS; //这两个return的东西要继承ActionSupport之后才能使用。 }else{ return INPUT; } } }
2、效果截图
2.1、2014_4_28_login_lx_01.jsp
2.2、2014_4_28_welcome_lx_01.jsp
相关文章推荐
- [原创]java WEB学习笔记56:Struts2学习之路---Struts 版本的 登录 demo
- struts2学习(6)自定义拦截器-登录验证拦截器
- Struts2的学习之路(一)
- Struts2的学习之路(二)
- Struts2的学习之路(四)配置文件加载顺序
- Struts2的学习之路(四)
- [原创]java WEB学习笔记59:Struts2学习之路---OGNL,值栈,读取对象栈中的对象的属性,读取 Context Map 里的对象的属性,调用字段和方法,数组,list,map
- Struts2的学习之路(一)
- Struts2的学习之路(二)
- Struts2的学习之路(三)
- Struts2的学习之路(四)配置文件加载顺序
- Struts2的学习之路(四)
- 菜鸟学习之路1 闪屏的实现及注册和登录界面之间的切换
- [原创]java WEB学习笔记62:Struts2学习之路--表单标签:form,表单标签的属性,textfield, password, hidden,submit ,textarea ,checkbox ,list, listKey 和 listValue 属性,select ,optiongroup ,checkboxlist
- Struts2学习之路_2_session的使用
- Struts2的学习之路(一)
- struts2学习笔记--拦截器(Interceptor)和登录权限验证Demo
- Struts2的学习之路(二)
- Struts2的学习之路(三)
- Struts2的学习之路(四)配置文件加载顺序