Struts 2

3 篇文章 0 订阅
2 篇文章 0 订阅

filter

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>struts2-test</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.ng.filter.StrutsPrepareAndExecuteFilter
	</filter-class>
   </filter>
   <filter-mapping>
   		<filter-name>Struts2</filter-name>
   		<url-pattern>/*</url-pattern> 
   </filter-mapping>
</web-app>

action配置

package name无所谓,action name 和登陆呼应

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="helloworld" extends="struts-default" namespace="/">
	<action name = "hello">
		<result name="success">/hello.jsp</result>
	</action>
	
	<action name = "1welcome1" class="com.struts2.WelcomeAction">
		<result name="success">/welcome.jsp</result>
		<result name="fail">/error.jsp</result>
	</action>
	</package>
</struts>

登陆jsp

hello.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>你好</title>
</head>
<body>
<form action="1welcome1.action" method="post"><!--"1welcome1"不加.action也行-->
	Please input your name:<input type="text" name="name" /><br />
        Please input your password<input type="password" name = "password" /><br />
	<input type = "submit" value="Submit"  /> 
</form>
</body>
</html>

显示jsp

welcome.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">
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>欢迎</title>
</head>
<body>
欢迎您 ${name}(${password})<br>
Hello World, <s:property value="name"/>, <s:property value="password"/>
</body>
</html>

action bean class

WelcomAction.java

package com.struts2;
public class WelcomeAction {
	private String name;
	private String password;
	public String execute(){
		System.out.println("WelcomeAction execute...");
		System.out.println("name="+name+",password="+password);
		if("".equals(name)){
			return "fail";
		}
		return "success";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值