struts2.5的小例子

第一步,导入工程所需jar包

我自己用的是struts-2.5.16-min-lib.zip,解压导入jar。

下载链接:https://pan.baidu.com/s/1f5NGEZfheJu1GvhVC3GYuQ 密码:o9v8

第二步,实现登录功能

package com.bingcao.test;

import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport {

	private String username;
	private String password;

	public String login() throws Exception {

        if (isInvalid(getUsername())) return INPUT;

        if (isInvalid(getPassword())) return INPUT;

		System.out.println("登录成功");
        return SUCCESS;
    }

    private boolean isInvalid(String value) {
        return (value == null || value.length() == 0);
    }


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

第三步,配置文件

Struts2配置

这里我用了引用文件的方式,所有有两个xml文件,struts.xml和example.xml。

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.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-allowed-methods>regex:.*</global-allowed-methods>
		
		<global-exception-mappings>
			<exception-mapping exception="java.lang.Exception"
				result="error" />
		</global-exception-mappings>
	</package>

	<include file="example.xml" />
	
	<!-- Add packages here -->
</struts>

example.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>
	<!-- 限制上传文件的大小,第一种方式:通过修改Struts2的常量。 在default.properties中配的Struts.multipart.maxSize=2097152 -->
	<constant name="struts.multipart.maxSize" value="5242880"></constant>
	<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
	
		
	<package name="topic" extends="default">
		
		<action name="Topic_*" class="com.bingcao.test.TopicAction" method="{1}">
			<result name="list">/index.jsp</result>
		</action>
		
	</package>
	
	<package name="user" extends="default">
		        
        <action name="Login_*" method="{1}" class="com.bingcao.test.LoginAction">
            <result>/test/Welcome.jsp</result>
            <result name="input">/test/Login.jsp</result>
        </action>
        
	</package>
		
</struts>

web.xml配置

从官方demo中看到,struts2.5有两个监听器,struts-prepare和struts-execute,目前自己分不清,都用上了。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>sh_tieba</display-name>
  
  <filter>
    <filter-name>struts-prepare</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
  </filter>
  <filter>
    <filter-name>struts-execute</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
    <filter-name>struts-prepare</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>struts-execute</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

第四步,jsp的登录页面

Login.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Sign On</title>
</head>

<body>
<s:form action="Login_login">
    <s:textfield key="username"/>
    <s:password key="password" />
    <s:submit/>
</s:form>
</body>
</html>

Welcome.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Welcome</title>
</head>

<body>
	<h3>登录成功</h3>
</body>
</html>

我的文件部署:

这里写图片描述

其中遇到的问题

原来是因为struts2.5为了提升安全性,添加了一个allowed-methods属性,此属性指定可被调用的方法。

allowed-methods属性还可以使用正则表达式配置:

<global-allowed-methods>regex:.*</global-allowed-methods>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
<?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" /> <package name="default" namespace="/" extends="struts-default"> <action name="helloworld" class="com.mytest.HelloWorldAction"> <result> /result.jsp </result> </action> </package> <package name="LoginForm" extends="struts-default"> <action name="login" class="com.mytest.LoginAction" method="execute"> <result> /login.jsp </result> </action> </package> </struts> <!--1.使用 struts2.5.16 版本 2.lib 文件夹下放置:工程所需jar包 3.xml标签库为远程获取,路径:http://struts.apache.org/dtds/struts-2.5.dtd 可设置为本地【xml输入语法快捷提示】,就不用远程获取了:window-->preference-->输入Catalog-->xml下的Catalog-->Add-->location:解压缩struts-core-2.5.16.jar 后,文件struts-2.5.dtd文件路径。 4.设置开发者模式: <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="utf-8" /> 每次HTTP请求系统都重新加载资源文件,有助于开发 5.struts配置文件改动后,是否重新加载 <constant name="struts.configuration.xml.reload" value="true" /> 6.查看源码:Build path 后的类库中,奶瓶图标找到struts-core-2.5.16.jar 右键-->properties-->java Source Attachment-->External location :源码路径 7.查看文档API:Build path 后的类库中,奶瓶图标找到struts-core-2.5.16.jar 右键-->properties-->javadoc location :输入网址 或选择源码DOC目录 8.拦截器:web.xml 配置拦截器<filter> struts2.5的filter-class 与struts2.5以前版本有所不同 <!-- 浏览器访问 http://localhost:8080/MyWeb/helloworld --> --> <?xml version="1.0" encoding="UTF-8"?> <web-app 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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置核心拦截器 --> <filter> <filter-name>struts2</filter-name> <!-- Filter的实现类 struts2.5 --> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <!-- 拦截所有的url --> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绀香之末

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值