Struts2学习之路_1_登录

来源:互联网 发布:前端开发优化问题 编辑:程序博客网 时间:2024/05/18 15:25

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











0 0