呵呵~一鼓作气!再弄点!HelloWorld不能一直下去吧!
HelloWorld里的struts.xml很简单。
- <package name="default" extends="struts-default">
- <action name="hello">
- <result >
- helloworld.jsp
- </result>
- </action>
- </package>
就是简单的判断当前的URL是否为/hello,如果是,则直接返回结果,在web中,结果的输出就是展现在浏览器中的页面!这里就是helloworld.jsp。此时,没有对输入进行任何操作。
我们就以最简单的用户登录作为例子。
首先创建一个Action。
package com.hello.model;
import com.opensymphony.xwork2.ActionSupport;
public class UserLoginAction extends ActionSupport {
private String userName ;
private String password ;
public String getUserName() {
System.out.println("getUserName");
return userName;
}
public void setUserName(String userName) {
System.out.println("setUserName");
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String add() {
System.out.println(userName + " has been accepted!");
return ActionSupport.SUCCESS;
}
}
里面的输出方便观察调用时机,只用看userName就好了!
将struts.xml修改下下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="login" class = "com.hello.model.UserLoginAction">
<result>
mainMenuContent.jsp
</result>
</action>
</package>
</struts>
index.html也修改下
<!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>Insert title here</title>
</head>
<body>
<a href="login?userName=zoe?password=56"">User Zoe Login!</a>
</body>
</html>
mainMenuContent.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Add operation successes!
</body>
</html>
运行后,发现输出setUserName。看来我们的输入没有白费,虽然此时的输入是我们之前就利用请求参数设置好了,但是也是小小的进步,之后可以用html的input~~
慢慢来~
在小小的修改下struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="login" class = "com.hello.model.UserLoginAction" method="add" >
<result>
mainMenuContent.jsp
</result>
</action>
</package>
</struts>
输出:
setUserName
zoe?password=56 has been accepted!
zoe?password=56 has been accepted!
目前对MVC有小小的感觉了!
Struts作为MVC中C(Controler),主要就是根据用户的输入,选择相应的操作并根据操作的结果输出。
Struts就是配置,就是个工具。
困了~睡觉了!