Struts2学习之路_1_登录

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











  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值