Controller 는 MVC의 개요에서 언급했듯이 Model과 View 사이의 중개자 (mediator/translator) 역할을 한다. Model과 View를 분리함으로써 Single domain model 기반의 다양한 프리젠테이션을 만들 수 있게 되고, 모든 클라이언트의 request가 Controller를 통하게 되므로 보안, 로깅 등 여러 공통 기능들을 제고할 수 있다. Anyframe Web 에서 제공하는 기능들이 대부분 여기에 해당한다.

| method | Description |
| processPreprocess() | Servlet 2.3에서 제공하는 Servlet Filter와 같은 역할을 한다. Request가 처리되기 전에 처리할 작업이 있다면 processPreprocess()에서 처리하면 된다. |
| processMapping() | 요청한 request에서 사용할 ActionMapping객체를 생성하는 과정이다. 만약 Path정보에 해당하는 Mapping정보가 없으면 에러가 포함된 response가 반환된다. |
| processRoles() | 사용자가 요청한 request를 처리할 수 있는 role이 있는지를 판별하는 과정이다. role이 존재한다면 과정이 계속 진행되겠지만 그렇지 않을 경우 에러를 발생시키며 처리과정이 중단된다. |
| processActionForm() | ActionMapping에 ActionForm이 설정되어 있다면 ActionForm 인스턴스를 생성한다. 생성된 인스턴스는 설정파일에서 설정한 Scope에 맞도록 저장하는 과정이다. 만약 새로운 ActionForm 인스턴스를 생성할 필요가 있다면 reset() method가 호출된다. |
| processPopulate() | request에서 전달되는 인자를 앞에서 생성된 ActionForm 인스턴스에 저장한다. |
| processValidate() | 설정파일에서 validate attribute가 true로 설정되어 있다면 ActionForm의 validate method를 호출하는 역할을 한다. request에 의하여 전달된 인자들의 유효성을 체크하는 과정이다. |
| processForward() | struts 설정파일의 action태그에서 forward나 include attribute가 설정되어 있다면 RequestDispatcher의 forward(), include() 메소드를 호출한다. 설정파일에 이 forward, include가 설정되어 있다면 request의 처리는 여기서 끝나고 해당 URL로 전달되게 된다. |
| processActionCreate() | 메소드 이름에서도 알 수 있듯이 request에 해당하는 Action 인스턴스를 생성한다. Action 인스턴스가 이미 존재하는지를 체크하여 존재한다면 재사용하게 된다. |
| processActionPerform() | 앞에서 생성한 Action 인스턴스의 execute()메소드를 호출하여 Action인스턴스를 실행시킨다. |
| processActionForward() | Action 인스턴스의 execute()를 실행한 결과 반환된 ActionForward에 해당하는 URL로 forward시키는 과정이다. |
<controller contentType="text/html;charset=UTF-8"
debug="3"
locale="true"
nocache="true"
processorClass="com.sds.banking.controller.SecureRequestProcessor" />
public class ConsolelogonAction extends DefaultAction
{
/**
* Method execute : 로그인을 시도한다.
* 로그인이 성공하면 session 유저정보를 저장한다.
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
ServiceContext ctx = ServiceContextFactory.getServiceContext();
Subject subject = new Subject();
// ActionErrors errors = new ActionErrors();
String userid = (String) PropertyUtils.getSimpleProperty(form, "userid");
String password = (String) PropertyUtils.getSimpleProperty(form, "password");
ApplicationContext ctx = getWebApplicationContext();
AuthenticationService authenticationService
= (AuthenticationService) ctx.getBean("securityService");
Credential c = new Credential();
c.setProperty("userid", userId);
c.setProperty("password", password);
subject = authenticationService.authenticate(c);
HttpSession session = request.getSession();
// 사용자의 정보를 세션에 저장한다.
session.setAttribute("subject", subject);
return (mapping.findForward("success"));
}
}
<action attribute="consolelogonForm" input="/index.jsp" name="consolelogonForm" path="/logon" type="anyframe.web.struts.common.console.ConsolelogonAction"> <exception key="error.password.mismatch" path="/index.jsp" type="javax.security.auth.login.AccountExpiredException" /> <exception key="error.password.mismatch" path="/index.jsp" type="javax.security.auth.login.FailedLoginException" /> <forward name="success" path="/getConfigs.do" redirect="true" /> </action>
public class UserAction extends DefaultAction
return mapping.findForward("success");

<action path="/empLoginView"
type="org.apache.struts.actions.ForwardAction"
parameter="/system/login.jsp">
</action>
<action
input="/subscription.jsp"
name="subscriptionForm"
path="/saveSubscription"
parameter="/path/to/processing/servlet"
scope="request"
type="org.apache.struts.actions.IncludeAction" />
</action>
public class ShoppingCartAction extends DispatchAction
{
public ActionForward add (
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
// TODO : add 기능 관련 로직
return mapping.findForward("add");
}
public ActionForward update (
...
// TODO : update 기능 관련 로직
return mapping.findForward("update");
}
public ActionForward search (
...
// TODO : search 기능 관련 로직
return mapping.findForward("search");
}
public ActionForward view (
...
// TODO : view 기능 관련 로직
return mapping.findForward("view");
}
}
<action
path="/cart"
input="/order/shoppingcart.jsp"
parameter="method"
scope="request"
type="com.test.action.ShoppingCartAction"
validate="false" />
<forward name="add" path="/order/shoppingcartAdd.jsp" />
<forward name="update" path="/order/shoppingcartUpdate.jsp" />
<forward name="search" path="/order/shoppingcartSearch.jsp"/>
<forward name="view" path="/order/shoppingcartView.jsp" />
</action>
http://localhost:8080/cart.do?method=add&id=2
<action path="/toModule"
type="org.apache.struts.actions.SwitchAction"/>
/toModule.do?prefix=/moduleB&page=/index.do
/toModule.do?prefix=&page=/index.do