Spring portlet 的配置过程:
1、 web.xml 配置
<servlet> <servlet-name>view-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>view-servlet</servlet-name> <url-pattern>/WEB-INF/servlet/view</url-pattern> </servlet-mapping>
2、*-portlet.xml 的配置
<bean class="net.ueye.test.DemoController"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <context:annotation-config/>
3、 DemoController
@Controller
@RequestMapping("view")
public class DemoController {
@RequestMapping
public String index(Model model){
model.addAttribute("accounts",new AccountService().findAll());
return "index";
}
@RequestMapping(params="action=edit")
public String edit(@RequestParam("id")String id,Model model){
model.addAttribute("account",new AccountService().get(id));
return "edit";
}
@RequestMapping(params="action=add")
public String newAccount(){
return "add";
}
@RequestMapping(params="action=create")
public void create(ActionResponse response,@ModelAttribute("acc")Account acc,Model model){
AccountService as=new AccountService();
as.insert(acc);
model.addAttribute("accounts",as.findAll());
response.setRenderParameter("action","index");
}
@RequestMapping(params="action=update")
public void update(ActionRequest request,ActionResponse response,Model model){
AccountService as=new AccountService();
as.get(request.getParameter("id")).setUsername(request.getParameter("username"));
model.addAttribute("accounts",as.findAll());
response.setRenderParameter("action","index");
}
@RequestMapping(params="action=delete")
public String delete(@RequestParam("id")String id,Model model){
AccountService as=new AccountService();
as.remove(id);
model.addAttribute("accounts",as.findAll());
return "index";
}
}
4、 index.jsp
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th colspan="2">Actions</th>
</tr>
<c:forEach var="account" items="${accounts }">
<tr>
<td>${account.id }</td>
<td>${account.username }</td>
<td><a href="<portlet:renderURL>
<portlet:param name="action" value="edit"/>
<portlet:param name="id" value="${account.id }"/>
</portlet:renderURL>">Edit</a>
</td>
<td><a href="<portlet:renderURL>
<portlet:param name="action" value="delete"/>
<portlet:param name="id" value="${account.id }"/>
</portlet:renderURL>">Delete</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="4">
<a href="<portlet:renderURL>
<portlet:param name="action" value="add"/>
<portlet:param name="id" value="${account.id }"/>
</portlet:renderURL>">Add</a>
</td>
</tr>
</table>
5、add.jsp
<form action="<portlet:actionURL/>" method="post">
<input type="hidden" name="action" value="create"/>
<table>
<tr>
<th>ID</th>
<td><input type="text" name="id" value="${account.id }" /></td>
</tr>
<tr>
<th>Name</th>
<th><input type="text" name="username"/></th>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="submit"/>
<input type="reset" value="reset"/>
</td>
</tr>
</table>
</form>