strus2超简单实例

一、项目整体结构图


二、配置web.xml

将上一章中并介绍的jar包导入项目

然后在项目的web.xml中添加如下配置

<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>
三、编写登陆界面(index.jsp)
   
  <body>
    <form action="log.action">
                      用户名:<input name="name"/>
                        密码:<input name="password"/>                    
     <input type="submit"/> 
    </form>       
  </body>
注意此处的action要与strus.xml中的action一致,并且此处没有修改   struts.action.extension = do 所以为log.action

四、配置strus.xml

<struts>
 <package name="struts_user" namespace="/" extends="struts-default">
 
    <action name="log" class="com.LogAction" >
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
</package>
</struts>    

该项必须添加,意识为配置struts2框架里的默认配置,如拦截器等

strus.xml中引入其他配置文件用 include,如: <include file="struts_admin.xml" />

这里namespace和action name的配置关系了action url 的访问,如此时为

五、编写action

public class LogAction {
	private String name;
	private String password;
	private String message;
	public String execute()  {
	if(this.name.equals("fuckTomorrow") && this.password.equals("yes")){
	       this.message="登录成功";
	       return "success";
	}
	else{
	      this.message="登录失败";
	      return "error";
	}
	}
	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;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	}
此处action获取请求参数用的是属性传值 
六、succes和error.jap

<body>
    <s:property value="message"/> 
  </body>
此处用的是struts2标签取值



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值