Spring portlet 的配置过程:
1、 web.xml 配置
1
<
servlet
>
2 < servlet-name > view-servlet </ servlet-name >
3 < servlet-class > org.springframework.web.servlet.ViewRendererServlet </ servlet-class >
4 < load-on-startup > 1 </ load-on-startup >
5 </ servlet >
6 < servlet-mapping >
7 < servlet-name > view-servlet </ servlet-name >
8 < url-pattern > /WEB-INF/servlet/view </ url-pattern >
9 </ servlet-mapping >
2 < servlet-name > view-servlet </ servlet-name >
3 < servlet-class > org.springframework.web.servlet.ViewRendererServlet </ servlet-class >
4 < load-on-startup > 1 </ load-on-startup >
5 </ servlet >
6 < servlet-mapping >
7 < servlet-name > view-servlet </ servlet-name >
8 < url-pattern > /WEB-INF/servlet/view </ url-pattern >
9 </ servlet-mapping >
2、*-portlet.xml 的配置
1
<
bean
class
="net.ueye.test.DemoController"
/>
2 < bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" >
3 < property name ="prefix" value ="/WEB-INF/jsp/" />
4 < property name ="suffix" value =".jsp" />
5 </ bean >
6 < context:annotation-config />
2 < bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" >
3 < property name ="prefix" value ="/WEB-INF/jsp/" />
4 < property name ="suffix" value =".jsp" />
5 </ bean >
6 < context:annotation-config />
3、 DemoController
1
@Controller
2 @RequestMapping( " view " )
3 public class DemoController {
4
5 @RequestMapping
6 public String index(Model model){
7 model.addAttribute("accounts",new AccountService().findAll());
8 return "index";
9 }
10
11 @RequestMapping(params="action=edit")
12 public String edit(@RequestParam("id")String id,Model model){
13 model.addAttribute("account",new AccountService().get(id));
14 return "edit";
15 }
16
17 @RequestMapping(params="action=add")
18 public String newAccount(){
19 return "add";
20 }
21
22 @RequestMapping(params="action=create")
23 public void create(ActionResponse response,@ModelAttribute("acc")Account acc,Model model){
24 AccountService as=new AccountService();
25 as.insert(acc);
26 model.addAttribute("accounts",as.findAll());
27 response.setRenderParameter("action","index");
28 }
29
30 @RequestMapping(params="action=update")
31 public void update(ActionRequest request,ActionResponse response,Model model){
32 AccountService as=new AccountService();
33 as.get(request.getParameter("id")).setUsername(request.getParameter("username"));
34 model.addAttribute("accounts",as.findAll());
35 response.setRenderParameter("action","index");
36 }
37
38 @RequestMapping(params="action=delete")
39 public String delete(@RequestParam("id")String id,Model model){
40 AccountService as=new AccountService();
41 as.remove(id);
42 model.addAttribute("accounts",as.findAll());
43 return "index";
44 }
45
46}
2 @RequestMapping( " view " )
3 public class DemoController {
4
5 @RequestMapping
6 public String index(Model model){
7 model.addAttribute("accounts",new AccountService().findAll());
8 return "index";
9 }
10
11 @RequestMapping(params="action=edit")
12 public String edit(@RequestParam("id")String id,Model model){
13 model.addAttribute("account",new AccountService().get(id));
14 return "edit";
15 }
16
17 @RequestMapping(params="action=add")
18 public String newAccount(){
19 return "add";
20 }
21
22 @RequestMapping(params="action=create")
23 public void create(ActionResponse response,@ModelAttribute("acc")Account acc,Model model){
24 AccountService as=new AccountService();
25 as.insert(acc);
26 model.addAttribute("accounts",as.findAll());
27 response.setRenderParameter("action","index");
28 }
29
30 @RequestMapping(params="action=update")
31 public void update(ActionRequest request,ActionResponse response,Model model){
32 AccountService as=new AccountService();
33 as.get(request.getParameter("id")).setUsername(request.getParameter("username"));
34 model.addAttribute("accounts",as.findAll());
35 response.setRenderParameter("action","index");
36 }
37
38 @RequestMapping(params="action=delete")
39 public String delete(@RequestParam("id")String id,Model model){
40 AccountService as=new AccountService();
41 as.remove(id);
42 model.addAttribute("accounts",as.findAll());
43 return "index";
44 }
45
46}
4、 index.jsp
1
<
table
>
2 < tr >
3 < th > ID </ th >
4 < th > Name </ th >
5 < th colspan ="2" > Actions </ th >
6 </ tr >
7 < c:forEach var ="account" items ="${accounts }" >
8 < tr >
9 < td > ${account.id } </ td >
10 < td > ${account.username } </ td >
11 < td >< a href ="<portlet:renderURL>
12 <portlet:param name=" action" value ="edit" />
13 < portlet:param name ="id" value ="${account.id }" />
14 </ portlet:renderURL > ">Edit </ a >
15 </ td >
16 < td >< a href ="<portlet:renderURL>
17 <portlet:param name=" action" value ="delete" />
18 < portlet:param name ="id" value ="${account.id }" />
19 </ portlet:renderURL > ">Delete </ a >
20 </ td >
21 </ tr >
22 </ c:forEach >
23 < tr >
24 < td colspan ="4" >
25 < a href ="<portlet:renderURL>
26 <portlet:param name=" action" value ="add" />
27 < portlet:param name ="id" value ="${account.id }" />
28 </ portlet:renderURL > ">Add </ a >
29 </ td >
30 </ tr >
31 </ table >
2 < tr >
3 < th > ID </ th >
4 < th > Name </ th >
5 < th colspan ="2" > Actions </ th >
6 </ tr >
7 < c:forEach var ="account" items ="${accounts }" >
8 < tr >
9 < td > ${account.id } </ td >
10 < td > ${account.username } </ td >
11 < td >< a href ="<portlet:renderURL>
12 <portlet:param name=" action" value ="edit" />
13 < portlet:param name ="id" value ="${account.id }" />
14 </ portlet:renderURL > ">Edit </ a >
15 </ td >
16 < td >< a href ="<portlet:renderURL>
17 <portlet:param name=" action" value ="delete" />
18 < portlet:param name ="id" value ="${account.id }" />
19 </ portlet:renderURL > ">Delete </ a >
20 </ td >
21 </ tr >
22 </ c:forEach >
23 < tr >
24 < td colspan ="4" >
25 < a href ="<portlet:renderURL>
26 <portlet:param name=" action" value ="add" />
27 < portlet:param name ="id" value ="${account.id }" />
28 </ portlet:renderURL > ">Add </ a >
29 </ td >
30 </ tr >
31 </ table >
5、add.jsp
1
<
form
action
="<portlet:actionURL/>"
method
="post"
>
2 < input type ="hidden" name ="action" value ="create" />
3 < table >
4 < tr >
5 < th > ID </ th >
6 < td >< input type ="text" name ="id" value ="${account.id }" /></ td >
7 </ tr >
8 < tr >
9 < th > Name </ th >
10 < th >< input type ="text" name ="username" /></ th >
11 </ tr >
12 < tr >
13 < td colspan ="2" >
14 < input type ="submit" value ="submit" />
15 < input type ="reset" value ="reset" />
16 </ td >
17 </ tr >
18 </ table >
19 </ form >
2 < input type ="hidden" name ="action" value ="create" />
3 < table >
4 < tr >
5 < th > ID </ th >
6 < td >< input type ="text" name ="id" value ="${account.id }" /></ td >
7 </ tr >
8 < tr >
9 < th > Name </ th >
10 < th >< input type ="text" name ="username" /></ th >
11 </ tr >
12 < tr >
13 < td colspan ="2" >
14 < input type ="submit" value ="submit" />
15 < input type ="reset" value ="reset" />
16 </ td >
17 </ tr >
18 </ table >
19 </ form >