Struts2 Template 通配符

一个Struts2简单登录实例,eclipse。

通配符实现两种登录方法

结构

基本配置

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>Struts2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>Struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>Struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

登录界面

Login.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<script>
		function LoginIdentity() {
			if (document.LoginForm.identity.value == "User") {
				document.LoginForm.action = "LoginUser"} 
			else if (document.LoginForm.identity.value == "Administrator") {
				document.LoginForm.action = "LoginAdministrator"}
			document.LoginForm.submit();
		}
</script>

<form name="LoginForm" method="post">
		<table align ="center" >
			<tr>
				<td align ="right"> Login: <input type= "text" name="account"><BR></td>
				<td><select name="identity">
    					<option value="User">User</option>
    					<option value="Administrator">Administrator</option></select></td>
			</tr>
			<tr>
				<td align ="right">Password:<input type= "password" name="passWord"><BR></td>
				<td><input type= "button" value="OK" onclick="LoginIdentity()"></td>
			</tr>
		</table>
		</form>
</body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
   "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
 <!-- 使用!方法时需要开启DMI功能 -->
 <!-- <constant name="struts.enable.DynamicMethodInvocation" value="true" /> --> 

   <package name="struts2" extends="struts-default">
      
<!--  Struts2.5特有问题,得允许内部访问。这个是全局的,可以单独写在action里
                但是如果写在action里的话就不能继承ActionSupport类了,并不明白。。。 -->
      <global-allowed-methods>regex:.*</global-allowed-methods>
      
      
      <action name="Login*" class="com.struts2.LoginAction"  method="{1}">
     	 <!-- 这个{1}代表的是前面的name属性中的第一个* -->
            <result name="usersuccess">/UserWelcome.jsp</result>
            <result name="adminsuccess">/AdministratorWelcome.jsp</result>
            <result name="failed">/ERRORR.jsp</result>
      </action>
   </package>
</struts>

Action的文件

LoginAction.java

package com.struts2;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

	private String account;
	private String passWord;
/*	private String identify;*/
	 
	   public String getPassWord() {
	    return passWord;
	}
	   
	   public void setPassWord(String passWord) {
	    this.passWord = passWord;
	}
	   
	   public void setAccount(String account) {
		      this.account = account;
		   }
	   
	   public String getAccount() {
		      return account;
		   }
	   
/*	   public void setIdentify(String identify) {
		      this.identify = identify;
		   }
	   
	   public String getIdentify() {
		      return identify;
		   }*/
	  
	public String Administrator() {
	    if (getPassWord().equals(getAccount())) {
		return "adminsuccess";}
	    else {
	    return "failed";}
	   }
	public String User() {
	    if (getPassWord().equals(getAccount())) {
		return "usersuccess";}
	    else {
	    return "failed";}
	   }
	}

然后瞎写成功界面/失败界面就行。记得在struts.xml里配置,OJBK。

猜你喜欢

转载自blog.csdn.net/Dalek11/article/details/82830587